Top 150-InterviewEinfach

Größtes Rechteck im Histogramm

Detaillierte Anleitung und Python-Implementierung für das Problem „Größtes Rechteck im Histogramm“.

Problemstellung

Einfach

Geben Sie anhand eines Arrays von Ganzzahlen heights, das die Balkenhöhe des Histogramms darstellt, wobei die Breite jedes Balkens 1 beträgt, die Fläche des größten Rechtecks im Histogramm zurück.

Schreiben Sie eine Funktion largestRectangleArea(heights: List[int]) -> int.

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

Beispiele

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?
Erwägen Sie die Verwendung stapelspezifischer Datenstrukturen wie Mengen oder Heaps.
Edge Cases to Watch
  • Leere Eingabestrukturen
  • Einzelelementeingaben
  • Große numerische Grenzen

Bereit zur Lösung?

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

Im Editor öffnen
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

Empfohlene Python-Ressourcen

Erweitern Sie Ihr Wissen mit zugehörigen interaktiven Tutorials, Spickzetteln und Codevergleichen.