stack = [] right = [len(nums)] * len(nums) left = [-1] * len(nums) for i inrange(len(nums)): while stack and nums[i] > nums[stack[-1]]: right[stack.pop()] = i left[i] = stack[-1] if stack else -1 stack.append(i) # 保存的是下一个最大的数对应的索引 print(right) # 右边比当前值大的第一个值的index print(left) # 左边比当前值大的第一个值的index
每日温度[739]
1 2 3 4 5 6 7 8 9 10
classSolution: defdailyTemperatures(self, temperatures: List[int]) -> List[int]: stack = [] res = [0]*len(temperatures) for i inrange(len(temperatures)): while stack and temperatures[i]>temperatures[stack[-1]]: c = stack.pop() res[c] = i - c stack.append(i) return res
下一个更大元素 I[496]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: defnextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: stack = [] d = {} for i inrange(len(nums2)): while stack and nums2[i]>nums2[stack[-1]]: d[nums2[stack.pop()]] = nums2[i] stack.append(i) #保存的是下一个最大的数对应的索引 res = [] for i in nums1: if i in d: res.append(d[i]) else: res.append(-1) return res
接雨水[42]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: deftrap(self, height: List[int]) -> int: defget_max_weight(height): left = [0] * len(height) # 注意:这里初始化为0 for i inrange(1, len(height)): left[i] = max(left[i-1], height[i-1]) # 注意:这里从height[i-1]对比 return left left_max = get_max_weight(height) right_max = get_max_weight(height[::-1])[::-1] print(height) print(left_max) print(right_max) res = 0 for i inrange(len(height)): res = res + max(0, min(left_max[i], right_max[i])-height[i]) return res
classSolution: defmaxArea(self, height: List[int]) -> int: left = 0 right = len(height) - 1
area = 0 while left <= right: temp = min(height[left], height[right]) * (right - left) if height[left] < height[right]: left = left + 1 else: right = right - 1 if temp > area: area = temp return area
# 单调队列 q, ret = deque(), [] for i, j inenumerate(nums): while q and nums[q[-1]] < j: q.pop() if q and q[0] <= i - k: q.popleft() q.append(i) if i >= k - 1: ret.append(nums[q[0]]) return ret
最小堆解法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: defmaxSlidingWindow(self, nums: List[int], k: int) -> List[int]: import heapq q = [] res = [] for i inrange(len(nums)): iflen(q) <= k - 1: heapq.heappush(q, (-nums[i], i)) else: res.append(-q[0][0]) while q and i - q[0][1] >= k: heapq.heappop(q) heapq.heappush(q, (-nums[i], i)) res.append(-q[0][0]) return res
classSolution: deftopKFrequent(self, nums: List[int], k: int) -> List[int]: from collections import defaultdict import heapq d = defaultdict(int) for i in nums: d[i] = d[i] + 1 cnt_key_value = [] for key, value in d.items(): cnt_key_value.append([value, key]) temp = [] for i in cnt_key_value: iflen(temp) < k: heapq.heappush(temp, i) else: if i[0] > temp[0][0]: heapq.heappop(temp) heapq.heappush(temp, i) return [i[1] for i in temp]
最大的K个就用最小堆,如果是最小的k个就要取负了
数组中的第K个最大元素[215]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: deffindKthLargest(self, nums: List[int], k: int) -> int: import heapq temp = [] for i inrange(len(nums)): iflen(temp)<k: heapq.heappush(temp, nums[i]) else: if nums[i]>temp[0]: heapq.heappop(temp) heapq.heappush(temp, nums[i]) return temp[0]
最大的K个就用最小堆,如果是最小的k个就要取负了
分割数组为连续子序列[659]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import heapq from collections import defaultdict
classSolution: defisPossible(self, nums): chains = defaultdict(list) for i in nums: ifnot chains[i-1]: heapq.heappush(chains[i],1) else: min_len = heapq.heappop(chains[i-1]) heapq.heappush(chains[i],min_len+1) for _,chain in chains.items(): if chain and chain[0] < 3: returnFalse returnTrue