-
Notifications
You must be signed in to change notification settings - Fork 0
33. Search in Rotated Sorted Array #45
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
|
|
||
| return left | ||
|
|
||
| min_index = find_min_index(nums) |
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.
bisect_left の範囲を一回変数に置く手もあります。
| first_ge_target_index = bisect_left( | ||
| nums, | ||
| target, | ||
| lo=lo, |
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.
ご存じかとは思いますが、
def insort_left(a, x, lo=0, hi=None, *, key=None):
と書かれたときloとhiは位置引数でも大丈夫なので、
bisect_left(nums, target, lo, hi)
で済みますね
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.
コメントありがとうございます。
気持ちとしては、読者がbisect_leftのシグネチャを熟知していなくても安心して読めるので、書くようにしています。
こんなコメントもありました。
Mike0121/LeetCode#45 (comment)
余談ですが、昔numpyのclipというメソッドをnp.clip([1,2,3], 2, 3)のつもりでnp.clip(2, 3, [1,2,3])と書いて酷い目にあった思い出があります。
|
PRのトップかDiscordに問題へのリンクを貼っていただけると助かります🙇♂️ |
| ``` | ||
| 思考ログ: | ||
| - targetの位置、middleの位置で場合わけをしていたら、よく分からなくなってしまった | ||
| - 一回で探索する方法が思いつかなくても、前回の153.の結果を使って最小値を求めたあと、もう一度探索する方法は思いつくはず |
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.
自分も初め一回で解こうとして場合分けがよくわからないことになりました。
| def is_le_last_val_and_ge_target(num): | ||
| return (num <= nums[-1], target <= num) | ||
|
|
||
| first_ge_target_index = bisect_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.
少し変数名が分かりにくく感じました。
lower_bound_index等はいかがでしょうか🙇♂️
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.
geの意味が、捉えられずでした。
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://ja.wikipedia.org/wiki/%E9%96%A2%E4%BF%82%E6%BC%94%E7%AE%97%E5%AD%90
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.
Tex でもたしか \leq, \geq があったかと思います。
| def is_between_min_val_and_last_val(num: int) -> bool: | ||
| return num <= nums[-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.
この一行の処理を関数化するメリットがわからずでした。
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.
あ、bisect_leftのkeyで使うためなのですね。なるほど。
| nums, | ||
| target, | ||
| lo=min_index, | ||
| hi=len(nums) | ||
| ) | ||
| else: | ||
| candidate_index = bisect.bisect_left( | ||
| nums, | ||
| target, | ||
| lo=0, | ||
| hi=min_index |
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.
pep8のコレに引っかかるかなと思ったんですけど、数えたら70文字でした。
ただそれでもまあまあ長いので、折り返してもいいかなと感じています。
| min_val_index = bisect_left( | ||
| nums, | ||
| True, | ||
| key=is_between_min_val_and_last_val |
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.
lambda を使ってもいいかもしれません。
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.
そうですね。無理やり名前をつけるくらいならこの程度の処理ならlambdaで処理したほうが綺麗でわかりやすいかもと思いました。
| def is_le_last_val_and_ge_target(num): | ||
| return (num <= nums[-1], target <= num) | ||
|
|
||
| first_ge_target_index = bisect_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.
Tex でもたしか \leq, \geq があったかと思います。
| ``` | ||
| 思考ログ: | ||
| - 無理せず2回探索するのが自然な気がしたのでこれに落ち着いた | ||
| - 最後の```candidate_index < len(nums) ```は必要ないのだけど、その確認を読み手にさせないためにも置いておくといいと思った |
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.
target <= nums[-1] のチェックがあるので candidate index が len(nums) にならないということですよね?
私としては candidate index < len(nums) のチェックがあるとむしろそうでない場合があるのかと思ってしまうので、チェックの代わりにコメントを残すか assert を入れるかなと思いました。
https://leetcode.com/problems/search-in-rotated-sorted-array/description/