-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/153. Find Minimum in Rotated Sorted Array #42
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
Satorien
wants to merge
1
commit into
main
Choose a base branch
from
solve/arai60_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,78 @@ | ||
| ## Step 1. Initial Solution | ||
|
|
||
| - 二分探索的にやるにはどうすれば良いか | ||
| - どこかの基準と比較? | ||
| - 不等号の関係性でどこに最小値あるか分かる | ||
| - 最初の要素が最小値の場合は例外処理 | ||
| - もっといい方法があれば後で実装する | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| if nums[0] < nums[-1]: | ||
| return nums[0] | ||
| left = 0 | ||
| right = len(nums) - 1 | ||
| while left + 1 < right: | ||
| middle = (left + right) // 2 | ||
| if nums[left] < nums[middle]: | ||
| left = middle | ||
| else: | ||
| right = middle | ||
| return nums[right] | ||
|
|
||
| ``` | ||
|
|
||
| ### Complexity Analysis | ||
|
|
||
| - 時間計算量:O(log n) | ||
| - 空間計算量:O(1) | ||
|
|
||
| ## Step 2. Alternatives | ||
|
|
||
| - 最大値のところを踏み越えて最小値に乗ったら返すという方が自然かもしれない | ||
| - https://github.com/tokuhirat/LeetCode/pull/42/files#diff-88c7c13d92168adf69c9cdf4e197359b856835b1b48d08927346a143bf8f7a55R17 | ||
| - https://github.com/olsen-blue/Arai60/pull/42/files#diff-856251eccb601f9962fc7fdd308675f5690975413b45fe6be0950672570bc6caR44 | ||
| - これをもとになるべくシンプルに書く | ||
| - 場合分けすると確かにこれで十分 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| left = 0 | ||
| right = len(nums) - 1 | ||
| while left < right: | ||
| middle = (left + right) // 2 | ||
| if nums[middle] < nums[right]: | ||
| right = middle | ||
| else: | ||
| left = middle + 1 | ||
| return nums[left] | ||
| ``` | ||
|
|
||
| - リスト内に重複がある場合はどうするか | ||
| - http://github.com/hroc135/leetcode/pull/40/files#diff-04f2110efff2f6666fa57b541ac1fa1b1de77a8edc09704f6266cc71cb7420c6R110 | ||
| - https://github.com/olsen-blue/Arai60/pull/42/files#diff-856251eccb601f9962fc7fdd308675f5690975413b45fe6be0950672570bc6caR82 | ||
| - 二分探索だと重複要素のどれに最初に当たるか分からない | ||
| - 基本は後ろ側を返すような処理になっている | ||
| - 見ている範囲(left, right, middle)で大小関係が分からない可能性がある | ||
| - 最初のif文でrightを更新すると戻れない | ||
| - 同じような形式で実装するのは難しそう | ||
|
|
||
| ## Step 3. Final Solution | ||
|
|
||
| - シンプルな実装に落ち着いた | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| left = 0 | ||
| right = len(nums) - 1 | ||
| while left < right: | ||
| middle = (left + right) // 2 | ||
| if nums[middle] < nums[right]: | ||
| right = middle | ||
| else: | ||
| left = middle + 1 | ||
| return nums[left] | ||
| ``` | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 別のところで講師の方されていた質問です。考えてみると理解が深まるかもしれません!
|
||
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.
探す値が変わるのは二分探索っぽくないないように感じました。