150 najlepszych wywiadówŁatwe

Największy prostokąt na histogramie

Szczegółowy przewodnik i implementacja Python dla problemu „Największy prostokąt w histogramie”.

Oświadczenie o problemie

Łatwe

Biorąc pod uwagę tablicę liczb całkowitych heights reprezentującą wysokość słupka histogramu, gdzie szerokość każdego słupka wynosi 1, zwróć obszar największego prostokąta na histogramie.

Napisz funkcję largestRectangleArea(heights: List[int]) -> int.

Ograniczenia
  • 1 <= len(heights) <= 10^5
  • 0 <= heights[i] <= 10^4

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla stosu, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.