Conversation
mamo3gr
reviewed
Feb 17, 2026
Comment on lines
+67
to
+71
| if (nums[middle] >= target) { | ||
| right = middle; | ||
| } else { | ||
| left = middle + 1; | ||
| } |
There was a problem hiding this comment.
(完全に好みの範囲ですが)
個人的には、半開区間で書くと、leftとrightの更新式が非対称になりミスが増えそうな印象を受けました。
mamo3gr
reviewed
Feb 17, 2026
Comment on lines
+103
to
+112
| while (left + 1 < right) { | ||
| int middle = left + (right - left) / 2; | ||
| if (nums[middle] >= target) { | ||
| right = middle; | ||
| } else { | ||
| left = middle; | ||
| } | ||
| } | ||
|
|
||
| return right; |
There was a problem hiding this comment.
個人的には、この開区間のバージョンが一番しっくりきています。
left 自身とそれよりも左がNG, right 自身とそれよりも右がOKで、left と right が隣り合うまでループして、最後はOKの左端=right を返す、というのが分かりやすかったです。
oda
reviewed
Feb 17, 2026
| - leftより左はtargetより小さく、rightを含めて右はtarget以上になるので、left == rightのとき、targetがあったらそこが答えになるし、targetがなくてもそこに挿入すればいい | ||
|
|
||
| - 上記の書き方で解いてみた | ||
| - geminiのstep1のコードレビューで、リストが空の場合は0を返すべき、middleがオーバーフローになると指摘されたのでそれを基に改善した |
There was a problem hiding this comment.
こういうのは決め次第なのですが、数式上の振る舞いが整っている方が後々都合がいいことが多いです。
sort(nums + [target]) したときの target のインデックスと思うと0が自然かなと思いますね。
| - この説明は結構腑に落ちた。これをtrueの中で繰り返していくイメージかな | ||
| > まず最初に問題の設定についてですが、1. [false, false, ..., false, true, true, ..., true] の切れ目を探す。 2. [false, false, ..., false, true, true, ..., true] のうち一番初めの true を探す。 | ||
| といった設定が多いと思います。 | ||
| - 今回の問題は切れ目を探す問題か。 |
There was a problem hiding this comment.
mamo3gr
reviewed
Feb 17, 2026
Comment on lines
+131
to
+140
| while (left <= right) { | ||
| int middle = left + (right - left) / 2; | ||
| if (nums[middle] >= target) { | ||
| right = middle - 1; | ||
| } else { | ||
| left = middle + 1; | ||
| } | ||
| } | ||
|
|
||
| return left; |
There was a problem hiding this comment.
個人的にはこのパターンが理解に時間がかかりました。「left が right を追い越したときに left が答え」というのが腹落ちしにくかったです。
|
コードには特に違和感ありませんでした。複数のパターンを試しているのも素晴らしいと思いました。 |
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.
今回の問題
https://leetcode.com/problems/search-insert-position/
次の問題
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/