Top 150 InterviewЛегко

Largest Rectangle In Histogram

Detailed guide and Python implementation for the 'Largest Rectangle In Histogram' problem.

Постановка задачи

Легко

Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram.

Write a function 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?
Consider using Stack-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Готовы решить?

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

Расширьте свои знания с помощью соответствующих интерактивных руководств, шпаргалок и сравнений кода.

Учебник по Python

Python Try/Except и обработка ошибок

Предотвратите сбой ваших скриптов Python. Узнайте, как блокировать try, кроме, наконец, и как правильно создавать пользовательские исключения.

Посмотреть ресурс
Практическое руководство

Как перевернуть строку в Python

Узнайте, как перевернуть строку в Python с помощью нарезки, функции Reverse() и конкатенации циклов, с помощью визуальных примеров кода.

Посмотреть ресурс
Шпаргалка

Шпаргалка по строковым методам Python

Полное справочное руководство по манипулированию строками в Python. Мастер форматирования, поиска, разделения, замены и проверки свойств строк.

Посмотреть ресурс
Сравнение языков

Python против JavaScript: какой язык программирования лучше?

Всестороннее сравнение Python и JavaScript. Изучите синтаксические различия, производительность, варианты использования (серверная и клиентская части) и примеры кодирования.

Посмотреть ресурс