本文主要是介绍Python | Leetcode Python题解之第84题柱状图中最大的矩形,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution:def largestRectangleArea(self, heights: List[int]) -> int:n = len(heights)left, right = [0] * n, [n] * nmono_stack = list()for i in range(n):while mono_stack and heights[mono_stack[-1]] >= heights[i]:right[mono_stack[-1]] = imono_stack.pop()left[i] = mono_stack[-1] if mono_stack else -1mono_stack.append(i)ans = max((right[i] - left[i] - 1) * heights[i] for i in range(n)) if n > 0 else 0return ans
这篇关于Python | Leetcode Python题解之第84题柱状图中最大的矩形的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!