Skip to content

Conversation

@Fuminiton
Copy link
Owner

while lo < hi:
mid = (lo + hi) // 2
if nums[mid] <= nums[-1]:
hi = mid
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここ少し違和感ありました。
この問題の答えになる範囲は0からlen-1で、
137行目でhiをlenにしているので、hiは答えにならない最大のindexだと予想しました。
ただここでhi=midとすると、答えになりうる最大のindexに意味が変わってしまっているように思いました。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私はloとhiはずっと同じ意味であってほしいと思いましたが、一般的なのかどうかはわからないです。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは、考え方次第で、「条件を満たす一番左の x を探す」という文脈では、条件を満たすものがないときに、len(nums) が返ってくるコードを書くのがよくあります。
ただ、今回は条件が nums[x] <= nums[-1] なので、必ず一番右端は条件を満たしますね。

hi = mid - 1
else:
lo = mid + 1

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メモ
[5613]でやってみます
lo hi mid
0 3 2
0 1 1
2 1  ループ終了

面白い動きをするなとおもいました。
おそらくmidの切り上げで動くソースを書いたのだと思いますが、探索範囲に答えがない二分探索も書けるんですね。

Copy link
Owner Author

@Fuminiton Fuminiton May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます。
ここ、探索範囲が狭まるだろうくらいの認識で、きちんとシミュレーションできていませんでした。

切り下げ/切り上げの選択は自信を持って説明できないので、もう少し考えてみます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants