Skip to content

33. Search in Rotated Sorted Array#41

Open
tom4649 wants to merge 4 commits intomainfrom
33.Search-in-Rotated-Sorted-Array
Open

33. Search in Rotated Sorted Array#41
tom4649 wants to merge 4 commits intomainfrom
33.Search-in-Rotated-Sorted-Array

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 31, 2026

Comment thread 33/sol1.py
minimum_index = left

# Step 2: Find the target
left_in_sorted = (
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名が長くて説明的すぎるため、逆にノイズになっているように感じました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ご指摘の通りだと思います。left, rightを使い回す形にしました

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ちなみにですが、古いコードを上書きしないことが推奨されています。
https://discord.com/channels/1084280443945353267/1084283898617417748/1488169544970404032

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

あ、すみません。sol1_revised.pyを用意してこちらのみ修正した気でいたのですがsol1.pyにも変更を反映してしまいました。sol1.pyは元に戻しておきました。

Comment thread 33/sol1.py
minimum_index = left

# Step 2: Find the target
left_in_sorted = (
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら、 target と nums[0] を比較し、 target が前半後半のどちらにあるかを判定し、 left と right の初期値を調整すると思います。

if nums[0] <= target:
    left, right = 0, left - 1
else:
    left, right = left, len_nums - 1

left と right を再利用しているのがやや汚く感じはします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

この置き方でコードが回るのは理解できました。
ただ個人的にはこの工夫をしても二分探索の回数は一回しか変わらないので今回は今のやり方を採用しようと思います。
初期化のleft, righの初期化を端以外にも可能性があることは頭に入れておこうと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

意図が伝わっておらず申し訳ありません。 (mid_in_sorted + minimum_index) % len_nums という計算が省け、読み手が理解しやすくなるだろうという意図がありました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

なるほど。通常の二分探索にしてインデックス操作を簡潔にするという意図ですね。
nums[-1] < targetの比較で場合分けして使わせていただきました。

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.

3 participants