Document algorithm for finding minimum in rotated array#14
Open
yakataN wants to merge 1 commit into
Open
Conversation
tokuhirat
reviewed
Sep 17, 2025
| #### step2 | ||
| https://github.com/KentaroJay/Leetcode/pull/18/files | ||
| https://github.com/Satorien/LeetCode/pull/42/files | ||
| - 半開区間じゃないコードを読み慣れていない。特に停止するかどうかが読み切れない。 |
There was a problem hiding this comment.
https://discord.com/channels/1084280443945353267/1196498607977799853/1269532028819476562
リンク先の考え方で整理すると色々なパターンでも対応できるようになると思います。
There was a problem hiding this comment.
Kaichi-Irie/leetcode-python#15 (comment)
この辺りも参考になると思います。
tokuhirat
reviewed
Sep 17, 2025
| - また、変数targetが必要なくなる | ||
| - この場合左が開いている半開区間で考えているから、最初から単調増加の場合も考えるならばleft = -1にする必要がある。 | ||
| - left_index -> left, right_index -> rightにしてもいいかもしれない。上から下に読むことを考えると、この変数はindexとしての振る舞いをしますというのが上の段階でわかるのは嬉しいかもしれないが、2行後にはその使い方をされているので。 | ||
| - indexといいつつ、left_index = -1とかくと違和感があるし、python の場合は最終項とも読めるので、ないほうがいいか。 |
There was a problem hiding this comment.
この違和感を解消するのであれば、左が閉じている区間で考えれば良いと思います。
tokuhirat
reviewed
Sep 17, 2025
| ```python | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| left, right = -1, len(nums) - 1 |
potrue
reviewed
Sep 18, 2025
| right_index = len(nums) - 1 | ||
| while right_index - left_index > 1: | ||
| mid_index = (left_index + right_index) // 2 | ||
| if nums[mid_index] > target: |
There was a problem hiding this comment.
>=のほうが自然だと思います。(この条件に関してTrueになる要素とFalseになる要素が並んでいる配列だとnumsを捉えることができるので)
実際には、mid_indexが0になる頃にはwhileが回っていないので動作はすると思うのですが。
There was a problem hiding this comment.
targetをnums[-1]にして、nums[mid_index] > targetとするとif nums[0] < nums[-1]の場合分けがいらなくなるかもしれません。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This problem:
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
Next problem:
https://leetcode.com/problems/move-zeroes/