Skip to content

Conversation

@TORUS0818
Copy link
Owner

@TORUS0818 TORUS0818 commented Mar 27, 2025


return left

min_index = find_min_index(nums)
Copy link

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,

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)
で済みますね

Copy link
Owner Author

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])と書いて酷い目にあった思い出があります。

@Ryotaro25
Copy link

PRのトップかDiscordに問題へのリンクを貼っていただけると助かります🙇‍♂️
https://leetcode.com/problems/search-in-rotated-sorted-array/description/

```
思考ログ:
- targetの位置、middleの位置で場合わけをしていたら、よく分からなくなってしまった
- 一回で探索する方法が思いつかなくても、前回の153.の結果を使って最小値を求めたあと、もう一度探索する方法は思いつくはず

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(

Choose a reason for hiding this comment

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

少し変数名が分かりにくく感じました。
lower_bound_index等はいかがでしょうか🙇‍♂️

Choose a reason for hiding this comment

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

geの意味が、捉えられずでした。

Copy link
Owner Author

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

Copy link

Choose a reason for hiding this comment

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

Tex でもたしか \leq, \geq があったかと思います。

Comment on lines +140 to +141
def is_between_min_val_and_last_val(num: int) -> bool:
return num <= nums[-1]

Choose a reason for hiding this comment

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

この一行の処理を関数化するメリットがわからずでした。

Choose a reason for hiding this comment

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

あ、bisect_leftのkeyで使うためなのですね。なるほど。

Comment on lines +224 to +234
nums,
target,
lo=min_index,
hi=len(nums)
)
else:
candidate_index = bisect.bisect_left(
nums,
target,
lo=0,
hi=min_index
Copy link

@olsen-blue olsen-blue Mar 29, 2025

Choose a reason for hiding this comment

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

好みですが、縦の移動は処理が進む時、という固定観念や偏見みたいなものが自分の中にあるので、横並びで書きたいかもしれないです。

Copy link
Owner Author

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
Copy link

Choose a reason for hiding this comment

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

lambda を使ってもいいかもしれません。

Copy link
Owner Author

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(
Copy link

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) ```は必要ないのだけど、その確認を読み手にさせないためにも置いておくといいと思った
Copy link

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 を入れるかなと思いました。

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.

7 participants