We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3d15a8b commit 84f190eCopy full SHA for 84f190e
kotlin/84-Largest-Rectangle-In-Histogram.kt
@@ -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
22
+ max = Math.max(max, curr[1] * (heights.size - curr[0]))
23
24
25
+ return max
26
27
+}
0 commit comments