-
Notifications
You must be signed in to change notification settings - Fork 0
153. Find Minimum in Rotated Sorted Array #44
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
skypenguins
wants to merge
1
commit into
main
Choose a base branch
from
leetcode/arai60/problem-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,128 @@ | ||
| # 153. Find Minimum in Rotated Sorted Array | ||
| - 問題: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ | ||
| - 言語: Python | ||
|
|
||
| ## Step1 | ||
| - 回転された配列から最小値を $O(\log n)$ で返す | ||
| ### 方針 | ||
| - 配列から最小値を返す問題と同じだと思い、以下で解答 | ||
|
|
||
| ### AC(?) | ||
| ```py | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| return min(nums) | ||
| ``` | ||
| - 所要時間: 0:10 | ||
| - 多分 `min()` に相当する処理を書かないといけないのだと思った | ||
|
|
||
| ### 方針 | ||
| - 配列の値の最大値(変曲点) `pivot` の次の値が最小値である | ||
| - `pivot` を線形に探すと最悪時間計算量 $O(n)$ となり、 $O(\log n)$ を達成できない | ||
| - 線形でいいならそもそも初めから最小値を探せば良い | ||
| - 回転した回数が判明すれば、自ずと最小値は判明する | ||
| - 回転した回数をどう求めるか? | ||
| - 例えば: `[11,13,15,17]` は4回だが、どう求めるか? | ||
| - 15分経過していたので正答を見る | ||
|
|
||
| ### とりあえずの回答(WA) | ||
| ```py | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| mid = len(nums) // 2 | ||
| min_n = float("inf") | ||
|
|
||
| for i in range(mid, len(nums)): | ||
| if nums[i] < min_n: | ||
| min_n = nums[i] | ||
|
|
||
| return min_n | ||
| ``` | ||
|
|
||
| ### 正答 | ||
| ```py | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| left, right = -1, len(nums) - 1 | ||
| while right - left > 1: | ||
| mid = (left + right) // 2 | ||
| if nums[mid] > nums[right]: | ||
| left = mid | ||
| else: | ||
| right = mid | ||
| return nums[right] | ||
| ``` | ||
| - 回転した回数の情報は別にいらない | ||
| - 二分探索のアプローチで素直に考えれば良い | ||
| #### 方針 | ||
| - 半開区間 `(left, right]` | ||
| - 不変条件: `right` は常に現時点での最小値の候補 | ||
| - `right` は必ず右半分側に留まる | ||
| - `left` は必ず左半分側に留まる | ||
|
|
||
| ## Step2 | ||
| - 典型コメント集: https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.tzbo5j6mbqc2 | ||
|
|
||
| - CPythonの `min()` 実装: | ||
|
|
||
| - https://github.com/thonda28/leetcode/pull/7 | ||
| - 正答はこの方のものを参照 | ||
| - 二分探索の時に考えるべきこと: https://github.com/thonda28/leetcode/pull/7#issuecomment-3287682725 | ||
|
|
||
| - https://discord.com/channels/1084280443945353267/1230079550923341835/1233971372946882600 | ||
| - 閉区間 `[left, right]` | ||
| - | ||
| ```py | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| left, right = 0, len(nums) - 1 | ||
| while left < right: | ||
| mid = (left + right) // 2 | ||
| if nums[mid] > nums[right]: | ||
| left = mid + 1 | ||
| else: | ||
| right = mid | ||
| return nums[left] | ||
| ``` | ||
| - Pythonのbisect: https://docs.python.org/3/library/bisect.html | ||
| - 考察の選択肢を広げる | ||
|
|
||
| - https://discord.com/channels/1084280443945353267/1192736784354918470/1236853602262319125 | ||
| - Pythonのbisectを使った方法 | ||
| - 開区間の方法と同じ | ||
| - | ||
| ```py | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| return nums[bisect_left(nums, -nums[-1], key=lambda x: -x)] | ||
| ``` | ||
| - 等価コード | ||
| ```py | ||
| lo, hi = 0, len(nums) | ||
| while lo < hi: | ||
| mid = (lo + hi) // 2 | ||
| if nums[mid] > nums[-1]: | ||
| lo = mid + 1 | ||
| else: | ||
| hi = mid | ||
| return nums[lo] | ||
| ``` | ||
|
|
||
| ## Step3 | ||
| - 閉区間の方法 | ||
| ```py | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| left, right = 0, len(nums) - 1 | ||
| while left < right: | ||
| mid = (left + right) // 2 | ||
| if nums[mid] > nums[right]: | ||
| left = mid + 1 | ||
| else: | ||
| right = mid | ||
| return nums[left] | ||
| ``` | ||
| - 所要時間: | ||
| - 1回目: 2:09 | ||
| - 2回目: 1:27 | ||
| - 3回目: 1:36 | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
丁寧に実行時間の見積もりをしたほうが良いかと思います。
CPythonを見ると、minの計算量自体はO(N)のようです(Nはnumsの長さ)
https://github.com/python/cpython/blob/main/Python/bltinmodule.c#L2087
このアプローチのポイントは、「N が十分小さいのであれば、線形探索でも要求される実行時間内で処理が完了する、もしくは、線形探索の方が二分探索などの別の手法と比べて(実装が単純で)実行時間も速い」という点だと思います。
Nの長さがどの程度まではこの手法を採用し、それを超える場合は別の手法を検討すべきなのか、という観点で考えるとよいと思います。
また、将来的に numsがどの程度の大きさまで増える可能性があるのか、想定するアプリケーションやユースケースを踏まえて考えることも重要だと思います。