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 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。