Skip to content

Conversation

@docto-rin
Copy link
Owner

Copy link

@nanae772 nanae772 left a comment

Choose a reason for hiding this comment

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

全体的に読みやすかったです

else:
lo, hi = 0, min_index
target_index = bisect.bisect_left(nums, target, lo=lo, hi=hi)
if target_index < len(nums) and nums[target_index] == target:

Choose a reason for hiding this comment

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

上の考察では

i < hi を先にチェックすべき。

と書かれていますが、最終的にhiではなくtarget_index < len(nums)の比較にされたのは何か理由がありますでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

hiよりlen(nums)の方が認知負荷が少ないかな、となんとなく思い、そうしました。

(bisect_leftの上限値をとった場合はダメとするより、and nums[target_index] == targetを安全に行うためのフィルタリングを行い、全ての判定をand以降に任せる、みたいな感じでしょうか)

ですが、自分としてはどちらでも趣味の範囲内だと考えています。


class Solution:
def search(self, nums: List[int], target: int) -> int:
key_function = lambda x: (x <= nums[-1], x)

Choose a reason for hiding this comment

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

なるほど、このような解き方もあるのですね。勉強になりました。
コードは短くなりますが、読む側としては少しパズルになるため実装1~2のほうが読みやすいかなと思いました。

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