-
Notifications
You must be signed in to change notification settings - Fork 0
153-find-minimum-in-rotated-sorted-array #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
05ryt31
wants to merge
1
commit into
main
Choose a base branch
from
feat/153
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| ## 問題文 | ||
| Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: | ||
|
|
||
| [4,5,6,7,0,1,2] if it was rotated 4 times. | ||
| [0,1,2,4,5,6,7] if it was rotated 7 times. | ||
| Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. | ||
|
|
||
| Given the sorted rotated array nums of unique elements, return the minimum element of this array. | ||
|
|
||
| You must write an algorithm that runs in O(log n) time. | ||
|
|
||
|
|
||
|
|
||
| Example 1: | ||
|
|
||
| Input: nums = [3,4,5,1,2] | ||
| Output: 1 | ||
| Explanation: The original array was [1,2,3,4,5] rotated 3 times. | ||
| Example 2: | ||
|
|
||
| Input: nums = [4,5,6,7,0,1,2] | ||
| Output: 0 | ||
| Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. | ||
| Example 3: | ||
|
|
||
| Input: nums = [11,13,15,17] | ||
| Output: 11 | ||
| Explanation: The original array was [11,13,15,17] and it was rotated 4 times. | ||
|
|
||
| ## step1で考えたこと | ||
| 1回rotateすると、一番後ろのnumberが先頭に来て、それ以降が一つずれる | ||
| 1ずつIndexをずらすには? | ||
| Binary Searchを使った解法が思いつかない | ||
|
|
||
| return the minimum element of this array. | ||
| -> この部分に関してはsortしたListの先頭をただreturnすれば良いのでは? | ||
|
|
||
| ## step1でのコード | ||
| ```py | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| sorted_nums = sorted(nums) | ||
| min = sorted_nums[0] | ||
| rotate_time = 0 | ||
|
|
||
| while nums != sorted_nums: | ||
| sorted_nums = | ||
| rotate_time += 1 | ||
|
|
||
| return min | ||
|
|
||
| ``` | ||
|
|
||
| ## step2での気づき | ||
| やはり、大小を比較するときに、=を含めるか否かが判断できていない。 | ||
| returnの位置を間違えていた。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| sorted_nums = sorted(nums) | ||
| min = sorted_nums[0] | ||
| rotate_time = 0 | ||
|
|
||
| while nums != sorted_nums: | ||
| sorted_nums = | ||
| rotate_time += 1 | ||
|
|
||
| return min |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| ## 解答を見た後に自力で書いたコード | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| left = 0 | ||
| right = len(nums) - 1 | ||
|
|
||
| while left < right: | ||
| mid = (left + right) // 2 | ||
|
|
||
| if nums[mid] < nums[right]: | ||
| right = mid | ||
| else: | ||
| left = mid + 1 | ||
|
|
||
| return nums[left] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| left = 0 | ||
| right = len(nums) - 1 | ||
|
|
||
| while left < right: | ||
| mid = (left + right) // 2 | ||
|
|
||
| if nums[mid] < nums[right]: | ||
| right = mid | ||
| else: | ||
| left = mid + 1 | ||
|
|
||
| return nums[left] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
念のため確認させてください。 nums[right] の代わりに nums[0] と比較することで、同じような処理を書くことはできますか?また、 nums[-1] と比較することで、同じような処理を書くことはできますか?