Все задачи
Сложно Решено: 0 Процент решений: 0%

Largest Rectangle in a Histogram

Find the largest rectangle that fits under a histogram.

Условие

A histogram has bars of width 1 with the given heights. Find the area of the largest rectangle that lies entirely within the bars. A monotonic stack gives an O(n) solution.

Implement largest_rectangle(heights).

Input

heights: Bar heights from left to right.

Output

Return the largest area.

Ограничения

0 ≤ length ≤ 200,000; 0 ≤ height ≤ 1,000,000,000

Примеры

Пример 1
Ввод:
heights = [2, 1, 5, 6, 2, 3]

Ожидаемый вывод:
expected = 10
Ввод:
std::vector<long long> heights = {2, 1, 5, 6, 2, 3};

Ожидаемый вывод:
long long expected = 10;
Ввод:
long[] heights = new long[] {2, 1, 5, 6, 2, 3};

Ожидаемый вывод:
long expected = 10;

Пояснение

Bars 5 and 6 together give a 2-wide rectangle of height 5: area 10.

Пример 2
Ввод:
heights = [4]

Ожидаемый вывод:
expected = 4
Ввод:
std::vector<long long> heights = {4};

Ожидаемый вывод:
long long expected = 4;
Ввод:
long[] heights = new long[] {4};

Ожидаемый вывод:
long expected = 4;

Пояснение

A single bar of height 4 has area 4.

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

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