Problema
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.
Restrições
0 ≤ length ≤ 200,000; 0 ≤ height ≤ 1,000,000,000
Exemplos
Entrada:
heights = [2, 1, 5, 6, 2, 3]
Saída esperada:
expected = 10
Entrada:
std::vector<long long> heights = {2, 1, 5, 6, 2, 3};
Saída esperada:
long long expected = 10;
Entrada:
long[] heights = new long[] {2, 1, 5, 6, 2, 3};
Saída esperada:
long expected = 10;
Explicação
Bars 5 and 6 together give a 2-wide rectangle of height 5: area 10.
Entrada:
heights = [4]
Saída esperada:
expected = 4
Entrada:
std::vector<long long> heights = {4};
Saída esperada:
long long expected = 4;
Entrada:
long[] heights = new long[] {4};
Saída esperada:
long expected = 4;
Explicação
A single bar of height 4 has area 4.
Referências
Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.