150强访谈简单

直方图中最大的矩形

“直方图中最大的矩形”问题的详细指南和 Python 实现。

问题陈述

简单

给定一个整数数组 heights 表示直方图的条形高度,其中每个条形的宽度为 1,返回直方图中最大矩形的面积。

编写一个函数 largestRectangleArea(heights: List[int]) -> int

约束条件
  • 1 <= len(heights) <= 10^5
  • 0 <= heights[i] <= 10^4

示例

Example 1
Input
heights = [2, 1, 5, 6, 2, 3]
Output
10
Explanation

The largest rectangle has area 10, formed by bars at index 2 and 3 (heights 5 and 6), with width 2 and height 5.

Example 2
Input
heights = [2, 4]
Output
4
Explanation

The largest rectangle is the bar at index 1 with height 4 and width 1, giving area 4.

Need a Hint?
考虑使用特定于堆栈的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。