本文主要是介绍day_49,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
42. 接雨水
class Solution:def trap(self, height: List[int]) -> int:stack = []res = 0for i in range(len(height)):while(stack and height[i] > height[stack[-1]]):mid = stack.pop()if stack:h = min(height[i], height[stack[-1]]) - height[mid]w = i - stack[-1] - 1res += h * w stack.append(i)return res
84. 柱状图中最大的矩形
class Solution:def largestRectangleArea(self, heights: List[int]) -> int:heights.insert(0, 0)heights.append(0)stack = []res = 0for i in range(len(heights)):while(stack and heights[i] < heights[stack[-1]]):mid = stack.pop()if stack:area = (i - stack[-1] - 1) * heights[mid]res = max(area, res)stack.append(i)return res
这篇关于day_49的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!