-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/33. Search in Rotated Sorted Array #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| index = binary_search_from_left(nums, target, key=score_num) | ||
| if nums[index] == target: | ||
| return index | ||
| return -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
面白い書き方だと思いました。
num > nums[-1]はnum >= nums[0]としても動きますかね?
個人的にはscore_numは次のように書いてしまえば初見でもわかりやすくなるかもしれないと思いました。(今の書き方もエレガントで面白いですが)
if target > nums[-1]:
if num >= target or num <= nums[-1]:
return 1
if target <= nums[-1]:
if num >= target and num <= nums[-1]:
return 1
return 0There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
もしこうするならscore_numはis_past_targetみたいな名前のbooleanを返す関数にしてしまって、
if key(nums[middle]) < key(target)みたいなところはif not is_past_target(num)みたいな形にしてもいいかもしれません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>=nums[0] にしても動くとは思いますが元々昇順だった場合にも一つ目のフラグが立ってしまって、ちょっとわかりにくいかもしれないですね。イメージの仕方の問題だとは思います
今回は練習もかねてbinary_searchを実装していますが、bisect_leftなどの組み込み関数に入れるなら数値を出す方がイメージに近いのかなという印象です
| class Solution: | ||
| def search(self, nums: List[int], target: int) -> int: | ||
| def score_num(num: int) -> int: | ||
| return -(num > nums[-1]) + (num >= target) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ブール値を足すことに私は抵抗があります
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ドキュメントにも書かれてました。初めて知りました。
https://docs.python.org/3/library/stdtypes.html#typebool
In many numeric contexts, False and True behave like the integers 0 and 1, respectively. However, relying on this is discouraged; explicitly convert using int() instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど、こういうのをどこかで見てそういうものかと思ってましたがあまり推奨されないんですね
| left = middle | ||
| continue | ||
| return -1 | ||
| return left |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
私の理解不足かもしれませんが、left = 0, middle = 0 となった時に is_between の right-1 は -1 になり、結果的に正しく動いているように見えて不思議に思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ご指摘ありがとうございます。その通りですね
is_betweenでright = 0のときは別で処理した方が良いですね
| class Solution: | ||
| def search(self, nums: List[int], target: int) -> int: | ||
| def score_position(num: int) -> int: | ||
| return (num <= nums[-1]) + (num >= target) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(num <= nums[-1], num) で本当はいいみたいですね。
| ## Step 1. Initial Solution | ||
|
|
||
| - 始めは最小値が分かればそこからは二分探索するだけだと考えて最小値を二分探索→再度二分探索、という方針を考えた | ||
| - ただ、最小値が分かったところで結局分岐は沢山発生するのであまり意味がないことに気が付いた |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
よく考えたらこれでも行ける
結局nums[0]かnums[-1]とtargetを比べる処理は必要そうだが
問題文:https://leetcode.com/problems/search-in-rotated-sorted-array/description/