目录
一、35. 搜索插入位置
1.二分内置函数
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
# 内置函数
idx = bisect.bisect_left(nums, target)
return idx
2.二分
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
# 二分
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] >= target:
right = mid - 1
else:
left = mid + 1
return left
二、704. 二分查找
1.二分
class Solution:
def search(self, nums: List[int], target: int) -> int:
# 二分
n = len(nums)
left, right = 0, n - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] >= target:
right = mid - 1
else:
left = mid + 1
return left if left < n and nums[left] == target else -1
2.二分2
来自灵神题解(. - 力扣(LeetCode))。通过将范围缩小到[0, n - 2],简化结尾判断。
class Solution:
def search(self, nums: List[int], target: int) -> int:
# 二分2
# 通过将范围缩小到[0, n - 2],简化结尾判断
n = len(nums)
left, right = 0, n - 2
while left <= right:
mid = (left + right) // 2
if nums[mid] >= target:
right = mid - 1
else:
left = mid + 1
return left if nums[left] == target else -1
三、744. 寻找比目标字母大的最小字母
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
if letters[-1] <= target:
return letters[0]
left, right = 0, len(letters) - 1
while left <= right:
mid = (left + right) // 2
if letters[mid] > target:
right = mid - 1
else:
left = mid + 1
return letters[left]
四、2389. 和有限的最长子序列
1.滑动窗口+排序(没过)
超时了。
class Solution:
def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
# 滑动窗口+排序
# 时复O(nlogn),没过
n, m = len(nums), len(queries)
nums.sort()
cnt = 0
s = 0
idx = sorted(list(range(m)),key=lambda x: queries[x])
for i in idx:
while cnt < n:
if s <= queries[i]:
s += nums[cnt]
cnt += 1
queries[i] = cnt
return queries
2.前缀和 + 二分 + 排序
不会,来自灵神题解(. - 力扣(LeetCode))。通过使用前缀和,使数组变得有序。
class Solution:
def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
# 前缀和 + 二分 + 排序
nums.sort()
n = len(nums)
for i in range(1, n):
nums[i] += nums[i - 1]
for i, x in enumerate(queries):
left, right = 0, n - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] > x:
right = mid - 1
else:
left = mid + 1
queries[i] = left
return queries
五、1170. 比较字符串最小字母出现频次
1.哈希表 + 二分
class Solution:
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
# 哈希表 + 二分
def order(w:str):
# 哈希表
cnt = Counter(w)
for i in range(26):
v = cnt[chr(97 + i)]
if v != 0:
return v
# return 0
n = len(words)
words = list(map(order, words))
words.sort()
for i in range(len(queries)):
queries[i] = n - bisect.bisect_right(words, order(queries[i]))
return queries
2.后缀和
来自官方题解(. - 力扣(LeetCode))。
class Solution:
def order(self, w: str):
# 寻找最小
minchar = 'z'
cnt = 0
for c in w:
if c < minchar:
cnt = 1
minchar = c
elif c == minchar:
cnt += 1
return cnt
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
# 后缀和
# 长度不大于10,数量就不大于10
cnt = [0] * 12 # 计数
for w in words:
cnt[self.order(w)] += 1
# 后缀和, [1, 10]
for i in range(9, 0, -1):
cnt[i] += cnt[i + 1]
res = []
for s in queries:
res.append(cnt[self.order(s) + 1]) #大于
return res
完
感谢你看到这里!一起加油吧!