目录
学习目标
- 503.下一个更大元素II
- 42. 接雨水
学习内容
503.下一个更大元素II
503. 下一个更大元素 II - 力扣(LeetCode)https://leetcode.cn/problems/next-greater-element-ii/
class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n = len(nums)
stack = []
res = [-1]*n
for i in range(2*n):
while stack and nums[stack[-1]]<nums[i%n]:
e = stack.pop()
res[e] = nums[i%n]
stack.append(i%n)
return res
42. 接雨水
42. 接雨水 - 力扣(LeetCode)https://leetcode.cn/problems/trapping-rain-water/
class Solution:
def trap(self, height: List[int]) -> int:
#height = height[0]+height+height[-1]
n = len(height)
tmp = [0]*n
a = 0
for i in range(n):
a = max(a,height[i])
tmp[i]= a
a = 0
for i in range(n-1,-1,-1):
a = max(a,height[i])
tmp[i]=min(tmp[i],a)
res = sum(tmp)-sum(height)
return res