Conversation
| minimum_index = left | ||
|
|
||
| # Step 2: Find the target | ||
| left_in_sorted = ( |
There was a problem hiding this comment.
変数名が長くて説明的すぎるため、逆にノイズになっているように感じました。
There was a problem hiding this comment.
ご指摘の通りだと思います。left, rightを使い回す形にしました
There was a problem hiding this comment.
ちなみにですが、古いコードを上書きしないことが推奨されています。
https://discord.com/channels/1084280443945353267/1084283898617417748/1488169544970404032
There was a problem hiding this comment.
あ、すみません。sol1_revised.pyを用意してこちらのみ修正した気でいたのですがsol1.pyにも変更を反映してしまいました。sol1.pyは元に戻しておきました。
| minimum_index = left | ||
|
|
||
| # Step 2: Find the target | ||
| left_in_sorted = ( |
There was a problem hiding this comment.
自分なら、 target と nums[0] を比較し、 target が前半後半のどちらにあるかを判定し、 left と right の初期値を調整すると思います。
if nums[0] <= target:
left, right = 0, left - 1
else:
left, right = left, len_nums - 1left と right を再利用しているのがやや汚く感じはします。
There was a problem hiding this comment.
この置き方でコードが回るのは理解できました。
ただ個人的にはこの工夫をしても二分探索の回数は一回しか変わらないので今回は今のやり方を採用しようと思います。
初期化のleft, righの初期化を端以外にも可能性があることは頭に入れておこうと思います。
There was a problem hiding this comment.
意図が伝わっておらず申し訳ありません。 (mid_in_sorted + minimum_index) % len_nums という計算が省け、読み手が理解しやすくなるだろうという意図がありました。
There was a problem hiding this comment.
なるほど。通常の二分探索にしてインデックス操作を簡潔にするという意図ですね。
nums[-1] < targetの比較で場合分けして使わせていただきました。
https://leetcode.com/problems/search-in-rotated-sorted-array/description/