โจทย์
ดูฉบับแปล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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน