classSolution: defdailyTemperatures(self, temperatures: List[int]) -> List[int]: res = [0] * len(temperatures) st = [] # 从前往后进行遍历 for index, temp inenumerate(temperatures): # st不为空且温度大于栈顶元素 while st and temperatures[st[-1]] < temp: j = st.pop() # 题目中问的是下一个更高温度出现在几天之后 因此用下标之差表示即可 res[j] = index - j st.append(index) return res
496、下一个更大元素I
直接寻找
1 2 3 4 5 6 7 8 9 10 11
classSolution: defnextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: res = [-1]*len(nums1) # 寻找下一个更大的元素 for index,n1 inenumerate(nums1): startindex = nums2.index(n1) for n in nums2[startindex+1:]: if n>n1: res[index] = n break return res
classSolution: defnextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: st = [] res = [-1]*len(nums2) for index,num inenumerate(nums2): # print(index,num) while st and nums2[st[-1]]<num: j = st.pop() res[j] = num st.append(index) # 使用zip通过两个可迭代的对象构造字典 myhash = dict(zip(nums2,res)) ans = [] for n1 in nums1: ans.append(myhash[n1]) return ans
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defnextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: myhash = {} st = [] res = [] for index, n inenumerate(nums2): while st and nums2[st[-1]] < n: myhash[nums2[st.pop()]] = n st.append(index) for n1 in nums1: res.append(myhash.get(n1, -1)) return res
classSolution: defnextGreaterElements(self, nums: List[int]) -> List[int]: # 如何解决循环的问题? # 循环也就最多循环一轮到自己本身 # 用求余来表示 res = [-1] * len(nums) st = [] for index, n inenumerate(nums * 2): while st and nums[st[-1]] < n: res[st.pop()] = n st.append(index % len(nums)) return res
classSolution: deftrap(self, height: List[int]) -> int: # 双指针 n = len(height) if n<=2: return0 res = 0 lheight = [0]*n rheight = [0]*n lheight[0] = height[0] # 进行初始化 for i inrange(1,n-1): lheight[i] = max(lheight[i-1],height[i]) rheight[n-1] = height[n-1] # 进行初始化 for i inrange(n-2,-1,-1): rheight[i] = max(rheight[i+1],height[i]) # print(lheight,rheight) for i inrange(1,n-1): res += min(lheight[i],rheight[i])-height[i] return res
classSolution: deftrap(self, height: List[int]) -> int: st = [] res = 0 for index, h inenumerate(height): while st and height[st[-1]] < h: j = st.pop() if st: res += (index - st[-1] - 1) * (min(h, height[st[-1]]) - height[j]) if st and height[st[-1]] == h: st.pop() st.append(index) return res
classSolution: deflargestRectangleArea(self, heights: List[int]) -> int: # 暴力解法 sum_ = 0 for i inrange(len(heights)): left, right = i, i while left >= 0: if heights[left] < heights[i]: break left -= 1 while right < len(heights): if heights[right] < heights[i]: break right += 1 sum_ = max(sum_, (right - left - 1) * heights[i]) return sum_
minLeftIndex[0] = -1 for i inrange(1,n): t = i-1 while t>=0and heights[t]>=heights[i]: t = minLeftIndex[t] # 左移 minLeftIndex[i] = t # print(minLeftIndex) minRightIndex[n-1] = n for i inrange(n-2,-1,-1): t = i+1 while t<=n-1and heights[t]>=heights[i]: t = minRightIndex[t] minRightIndex[i] = t
classSolution: deflargestRectangleArea(self, heights: List[int]) -> int: # 双指针法 n = len(heights) minLeftIndex = [0]*n minRightIndex = [0]*n # 寻找左边的小于他的下标 minLeftIndex[0] = -1 for i inrange(1,n): t = i-1 while t>=0and heights[t]>=heights[i]: t = minLeftIndex[t] # 左移 minLeftIndex[i] = t # print(minLeftIndex) minRightIndex[n-1] = n for i inrange(n-2,-1,-1): t = i+1 while t<=n-1and heights[t]>=heights[i]: t = minRightIndex[t] minRightIndex[i] = t # print(minRightIndex) res = 0 for i inrange(n): res = max(res,(minRightIndex[i]-minLeftIndex[i]-1)*heights[i]) return res
classSolution: deflargestRectangleArea(self, heights: List[int]) -> int: # 单调栈 heights.insert(0,0) heights.append(0) stack = [0] res = 0 for i inrange(1,len(heights)): if heights[i]>heights[stack[-1]]: stack.append(i) elif heights[i]==heights[stack[-1]]: stack.pop() stack.append(i) else: while stack and heights[i] < heights[stack[-1]]: mid = stack.pop() if stack: left_index = stack[-1] right_index = i width = right_index - left_index - 1 height = heights[mid] res = max(res,width*height) stack.append(i) return res
优化版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution: deflargestRectangleArea(self, heights: List[int]) -> int: heights.append(0) heights.insert(0,0) st = [] res = 0 for index,h inenumerate(heights): while st and h<heights[st[-1]]: # 开始进行计算面积 mid = st.pop() if st: left = st[-1] right = index res = max(res,heights[mid]*(right-left-1)) if st and heights[st[-1]]==h: st.pop() st.append(index) return res
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution: deflargestRectangleArea(self, heights: List[int]) -> int: heights.insert(0, 0) heights.append(0) # print(heights) st = [] res = 0 for index inrange(len(heights)): while st and heights[st[-1]] > heights[index]: # 计算面积 j = st.pop() if st: res = max(res, (index - st[-1] - 1) * heights[j]) if st and heights[st[-1]] == heights[index]: st.pop() st.append(index) return res