-
Notifications
You must be signed in to change notification settings - Fork 0
Create 33. Search in Rotated Sorted Array.md #43
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
tokuhirat
wants to merge
1
commit into
main
Choose a base branch
from
33.-Search-in-Rotated-Sorted-Array
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
93 changes: 93 additions & 0 deletions
93
33. Search in Rotated Sorted Array/33. Search in Rotated Sorted Array.md
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,93 @@ | ||
| # 33. Search in Rotated Sorted Array | ||
| ## STEP1 | ||
| - 何も見ずに解いてみる | ||
| - [left, right] を候補とする。left = right の時 middle = left となり、target と比較される。等しくない場合には次の while 文は実行されず −1 が返る。範囲の狭め方としては、rotate 位置と middle と target の位置関係で場合分けした。 | ||
| ```python | ||
| class Solution: | ||
| def search(self, nums: List[int], target: int) -> int: | ||
| left = 0 | ||
| right = len(nums) - 1 | ||
| while left <= right: | ||
| middle = (left + right) // 2 | ||
| if nums[middle] == target: | ||
| return middle | ||
| if nums[middle] < target: | ||
| if nums[middle] >= nums[left]: | ||
| left = middle + 1 | ||
| continue | ||
| if target <= nums[right]: | ||
| left = middle + 1 | ||
| else: | ||
| right = middle - 1 | ||
| else: | ||
| if nums[middle] <= nums[right]: | ||
| right = middle - 1 | ||
| continue | ||
| if target >= nums[left]: | ||
| right = middle - 1 | ||
| else: | ||
| left = middle + 1 | ||
| return -1 | ||
| ``` | ||
|
|
||
| ## STEP2 | ||
| ### プルリクやドキュメントを参照 | ||
| - https://github.com/olsen-blue/Arai60/pull/43/files | ||
| - middle と right が指す数の大小関係を先に判定している。崖を超える前後がわかり、場合分けがシンプルになる。 | ||
| - https://discord.com/channels/1084280443945353267/1233295449985650688/1239594872697262121 | ||
| - https://discord.com/channels/1084280443945353267/1233295449985650688/1239446770761596928 | ||
| - 最初解こうとした時に、nums[middle] と target と nums[-1] の大小関係でどうにかしようと思って失敗していた。*2 をする発想がなかったので、崖を越えたのか、middle を越えたのか見分けがつけられなかった。なるほど、このようにするとできる方法があるのかと驚いた。 | ||
| - https://github.com/Yoshiki-Iwasa/Arai60/pull/36#discussion_r1712955053 | ||
| ここまで整理できていたらわかりやすいですね。ただ思いつきにくいなとも思う。 | ||
|
|
||
| 場合分けを整理して、2分探索の確認のため区間の取り方を変えて書いてみる。[left, right) に解の候補を含まれるように探索。 | ||
| ```python | ||
| class Solution: | ||
| def search(self, nums: List[int], target: int) -> int: | ||
| left = 0 | ||
| right = len(nums) | ||
| while left < right: | ||
| middle = (left + right) // 2 | ||
| if nums[middle] == target: | ||
| return middle | ||
| if nums[middle] > nums[-1]: | ||
| if nums[0] <= target < nums[middle]: | ||
| right = middle | ||
| else: | ||
| left = middle + 1 | ||
| else: | ||
| if nums[middle] < target <= nums[-1]: | ||
| left = middle + 1 | ||
| else: | ||
| right = middle | ||
| return -1 | ||
| ``` | ||
| - https://github.com/tokuhirat/LeetCode/pull/41/files#r2217335504 | ||
| - 探す数字が配列に含まれていない場合に -1 を返す場合、index の候補が一つになってもそれが探している値を指すか確認しないといけない(対照的なケースとして挿入箇所を探す場合)。これをwhile文の中でやると終了時は解の候補がなくなるため left > right になるのは当たり前だなとわかるようになった。ただ、挿入箇所を探す場合には、最後に候補indexが一つ残るようにする(解釈する)方が良さそうかなと思っています。 | ||
|
|
||
| ## STEP3 | ||
| ### 3回ミスなく書く | ||
| 個人的には閉区間で書いた方がわかりやすいです。 | ||
| ```python | ||
| class Solution: | ||
| def search(self, nums: List[int], target: int) -> int: | ||
| left = 0 | ||
| right = len(nums) - 1 | ||
| while left <= right: | ||
| middle = (left + right) // 2 | ||
| if nums[middle] == target: | ||
| return middle | ||
| if nums[middle] > nums[-1]: | ||
| if nums[0] <= target < nums[middle]: | ||
| right = middle - 1 | ||
| else: | ||
| left = middle + 1 | ||
| else: | ||
| if nums[middle] < target <= nums[-1]: | ||
| left = middle + 1 | ||
| else: | ||
| right = middle - 1 | ||
| return -1 | ||
| ``` | ||
|
|
||
| 3分,2分,2分で3回Accept | ||
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.
いいと思います。
が、個人的には回転位置を見つけたのちにソート済み配列(先頭から回転位置 or 回転位置から末尾)を探索する方針の方が理解しやすいかと思います
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.
コメントありがとうございます。書いてみました。わかりやすい方針だと思いました。