Open
Conversation
oda
reviewed
Nov 24, 2025
| def cmp(a, b): | ||
| return (a > b) - (a < b) | ||
| def priority(x): | ||
| return (nums[-1] < x) * -2 + cmp(x, target) |
There was a problem hiding this comment.
補足すると、タプルの1個目の判定で[False, ..., False, True, ..., True]というような単調述語で先に順序づけされます。(pythonのFalse, Trueはintに型変換すると0, 1になるので広義単調増加。)
あと、下記のように2回bisect_leftを使う書き方もあります。key=lambda x: (x <= nums[-1], x)でまとめず分割してやっていることに相当します。
import bisect
class Solution:
def search(self, nums: list[int], target: int) -> int:
min_index = bisect.bisect_left(nums, True, key=lambda x: x <= nums[-1])
if target <= nums[-1]:
lo, hi = min_index, len(nums)
else:
lo, hi = 0, min_index
target_index = bisect.bisect_left(nums, target, lo=lo, hi=hi)
if target_index < hi and nums[target_index] == target:
return target_index
return -1
naoto-iwase
reviewed
Nov 25, 2025
| def cmp(a, b): | ||
| return (a > b) - (a < b) | ||
| def priority(x): | ||
| return (nums[-1] < x) * -2 + cmp(x, target) |
There was a problem hiding this comment.
補足すると、タプルの1個目の判定で[False, ..., False, True, ..., True]というような単調述語で先に順序づけされます。(pythonのFalse, Trueはintに型変換すると0, 1になるので広義単調増加。)
あと、下記のように2回bisect_leftを使う書き方もあります。key=lambda x: (x <= nums[-1], x)でまとめず分割してやっていることに相当します。
import bisect
class Solution:
def search(self, nums: list[int], target: int) -> int:
min_index = bisect.bisect_left(nums, True, key=lambda x: x <= nums[-1])
if target <= nums[-1]:
lo, hi = min_index, len(nums)
else:
lo, hi = 0, min_index
target_index = bisect.bisect_left(nums, target, lo=lo, hi=hi)
if target_index < hi and nums[target_index] == target:
return target_index
return -1| right = mid - 1 | ||
|
|
||
| return -1 | ||
| ``` |
There was a problem hiding this comment.
自分なら下記のように書きます。
class Solution:
def search(self, nums: List[int], target: int) -> int:
"""
不変条件:
- leftより左はrotationエリアが違うか、targetより小さい
- rightより右はrotationエリアが違うか、target以上
区間 left <= x <= right のサイズが0になるまで探索する。
"""
left = 0
right = len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] > nums[-1] and target <= nums[-1]:
left = mid + 1
continue
if nums[mid] <= nums[-1] and target > nums[-1]:
right = mid - 1
continue
if nums[mid] < target:
left = mid + 1
else:
right = mid - 1
# right + 1 = left の状況。
# 不変条件より、targetと等しくなりうるのはnums[left]のみ。
if left < len(nums) and nums[left] == target:
return left
return -1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
33. Search in Rotated Sorted Array
次回予告: 39. Combination Sum