Skip to content

Commit 84f190e

Browse files
committed
Kotlin: 84. Largest Rectangle in Histogram
1 parent 3d15a8b commit 84f190e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
fun largestRectangleArea(heights: IntArray): Int {
3+
val st = LinkedList<IntArray>()
4+
st.push(intArrayOf(-1, 0))
5+
var max = 0
6+
7+
for (i in heights.indices) {
8+
var start = i
9+
10+
while (!st.isEmpty() && st.peek()[1] > heights[i]) {
11+
val curr = st.pop()
12+
val height = curr[1]
13+
start = curr[0]
14+
max = Math.max(max, height * (i - start))
15+
}
16+
17+
st.push(intArrayOf(start, heights[i]))
18+
}
19+
20+
while (!st.isEmpty()) {
21+
val curr = st.pop()
22+
max = Math.max(max, curr[1] * (heights.size - curr[0]))
23+
}
24+
25+
return max
26+
}
27+
}

0 commit comments

Comments
 (0)