Skip to content

33. Search in Rotated Sorted Array#32

Merged
ryosuketc merged 2 commits into
mainfrom
33_search_in_rotated_sorted_array
Jul 30, 2025
Merged

33. Search in Rotated Sorted Array#32
ryosuketc merged 2 commits into
mainfrom
33_search_in_rotated_sorted_array

Conversation

@ryosuketc

Copy link
Copy Markdown
Owner


```python
def priority(num, nums, target):
return (num <= nums[-1], target <= num, num)

@Fuminiton Fuminiton Jun 11, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

num <= nums[-1], numだけでも動くと思います。

  • 崖を登っている最中にtarget以上があるとき
    • 崖を登っている中でtarget以上になるはじめの位置を知りたい
  • 崖を越えたあとのどこかにtarget以上があるときは(正確には崖より大きくなることはないのでnums[-1]以下)
    • 崖を越えた中でtarget以上になるはじめの位置を知りたい

となるので、priorityにtargetを入れた結果と、priorityをbisect_leftに渡すイメージです。

自分は、全学年の生徒をクラス順にクラスごとの背の順で並ばせるソート(1年1組の背の順の後に1年2組の背の順が続く)から、指定した組の生徒で指定した身長以上の生徒がいるかという状況をイメージして、この問題でやりたいことが掴めました。

* `Solution3_WA`: 半開区間 (right は含まない) で書こうとして失敗。
* https://github.com/hayashi-ay/leetcode/pull/49/files
* `Solution3_AC`: right は含まないんだから、right = middle だ (middle を探索済みにする)。あと nums[right] も nums[right - 1]
* 慣れの問題かもしれないが、閉区間の方が書きやすい気がした

@fuga-98 fuga-98 Jun 11, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

半開区間で書けないということは、おそらく意味を取れていないので、開区間や閉区間という言葉を使わずに表現してみると良いと思います。

fuga-98/arai60#41 (comment)

#24 (comment)


### step3

* 特記なし。閉区間のパターンで練習。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

2回探索する方法も考えても良いと思いました。
崖を探したあとに数値を探すみたいなイメージです。


なので、target 自体を探しているわけではなく (target を探していると思っていた)、priority の返り値 = (bool, bool) を探している。ので、その返り値が出てくる一番左を返している。priority(target) は、定義から FT か TT (後ろが target 以上で True なので) のどちらか。

うーん、やっていることはわかったのだが、正直なぜこれ (`priority`) で target の位置になるのかイマイチ納得はできていない。。

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はどちらの山に所属しているかチェックします。

山が異なっていたら違う山にいくよう範囲を狭めましょう。

山が一緒ならその山を二分探索しましょう。

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.

@Fuminiton さんのご説明と合わせてある程度理解できたような気がします。

@Fuminiton さんご指摘のように、priority をこのように定義すると

def priority(num, nums, target):
    return (num <= nums[-1], num)

nums の配列は keys のような配列になります。nums は sort されていませんが、崖の前か後か、という情報を加えることで、keys はソートされた状態になるので これに対して binary search することができます。

priority(5)(False, 5) なので、それを挿入できる一番左の位置 (今回は target が配列に存在するので 1) を返す、というわけですね。

nums=[4, 5, 6, 7, 0, 1, 2]
target=5
keys=[(False, 4), (False, 5), (False, 6), (False, 7), (True, 0), (True, 1), (True, 2)]

個人的に、nums 自体はソートされていないが、崖の前後の情報を加えることでソートした状態にできる、ということを理解できていなかったように思いました。

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://github.com/Yoshiki-Iwasa/Arai60/pull/36/files#r1712955053
* https://github.com/fuga-98/arai60/pull/43/files
* https://github.com/olsen-blue/Arai60/pull/43/files
* 面白いがこれは思いつかないかな…

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これは思いつかなくてもいいかと思います。

@ryosuketc ryosuketc added the comments reviewed Reviewe comments are examined. No major re-review of the problem is needed. label Jul 17, 2025
@ryosuketc
ryosuketc merged commit 465b9b8 into main Jul 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comments reviewed Reviewe comments are examined. No major re-review of the problem is needed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants