-
Notifications
You must be signed in to change notification settings - Fork 0
153. Find Minimum in Rotated Sorted Array #37
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
akmhmgc
wants to merge
1
commit into
main
Choose a base branch
from
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,32 @@ | ||
| # step1 何も見ずに解く | ||
| left,rightをそれぞれ配列の先頭、末尾を指すインデックスとする。 | ||
| leftの値よりもrightの値の方が大きければ昇順に並んでいるのでleftの値を返す。 | ||
|
|
||
| middle = (left + right) / 2とする | ||
| middleの値が | ||
| leftの値以上である場合、middleの値を含む左側には最小値より大きい値しかない。なのでleftをmiddle + 1として更新する。 | ||
| (最小値**以上**ではない理由は、先に昇順に並んでいる場合を処理しているため。) | ||
| leftの値よりも小さい場合、middleの値を含む右側には最小値以上の値しかない。なので、rightをmiddleとして更新する。 | ||
|
|
||
| なんかやけに複雑な気がするけど、しばらく考えて切り上げた。 | ||
| ```ruby | ||
| # @param {Integer[]} nums | ||
| # @return {Integer} | ||
| def find_min(nums) | ||
| left = 0 | ||
| right = nums.length - 1 | ||
|
|
||
| while left < right | ||
| return nums[left] if nums[left] < nums[right] | ||
|
|
||
| middle = (left + right) / 2 | ||
| if nums[middle] >= nums[left] | ||
| left = middle + 1 | ||
| else | ||
| right = middle | ||
| end | ||
| end | ||
|
|
||
| nums[left] | ||
| end | ||
| ``` |
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,38 @@ | ||
| # step2 他の方の解答を見る | ||
|
|
||
| ## 常に右端と比べる方法 | ||
| https://github.com/sakupan102/arai60-practice/pull/43 | ||
|
|
||
| これは非常にわかりやすい。 | ||
| rightを含む右側が最小値以上で、leftより左側が最小値より大きい。 | ||
| なので探索が終わった時点のleftおよびrightが最小値となる。 | ||
|
|
||
| ```ruby | ||
| # @param {Integer[]} nums | ||
| # @return {Integer} | ||
| def find_min(nums) | ||
| left = 0 | ||
| right = nums.length | ||
|
|
||
| while left < right | ||
| middle = (left + right) / 2 | ||
| if nums[middle] <= nums.last | ||
| right = middle | ||
| else | ||
| left = middle + 1 | ||
| end | ||
| end | ||
|
|
||
| nums[left] | ||
| end | ||
| ``` | ||
|
|
||
| `Array#bsearch`を使うと以下のように | ||
|
|
||
| ```ruby | ||
| # @param {Integer[]} nums | ||
| # @return {Integer} | ||
| def find_min(nums) | ||
| nums.bsearch { |val| val <= nums.last } | ||
| end | ||
| ``` |
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,19 @@ | ||
| # step3 3回続けて10分以内に書いてエラーを出さなければOKとする | ||
|
|
||
| ```ruby | ||
| # @param {Integer[]} nums | ||
| # @return {Integer} | ||
| def find_min(nums) | ||
| left = 0 | ||
| right = nums.size | ||
| while left < right | ||
| middle = (left + right) / 2 | ||
| if nums[middle] <= nums.last | ||
| right = middle | ||
| else | ||
| left = middle + 1 | ||
| end | ||
| end | ||
| nums[left] | ||
| end | ||
| ``` | ||
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 @@ | ||
| ## step4 レビューを受けて解答を修正 |
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.
良いと思いました。