-
Notifications
You must be signed in to change notification settings - Fork 5
/
84. Largest Rectangle in Histogram.java
48 lines (48 loc) · 1.14 KB
/
84. Largest Rectangle in Histogram.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
final int n = heights.length;
int[] walls = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = n - 1; i >= 0; i--) {
int curHeight = heights[i];
while (stack.isEmpty() == false && heights[stack.peek()] >= curHeight)
stack.pop();
if (stack.isEmpty() == true)
walls[i] = n;
else
walls[i] = stack.peek();
stack.push(i);
}
return walls;
}
private int[] getLeftHeightIndcies(int[] heights) {
// TODO Auto-generated method stub
final int n = heights.length;
int[] walls = new int[n];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < n; i++) {
int curheight = heights[i];
while (stack.isEmpty() == false && heights[stack.peek()] >= curheight)
stack.pop();
if (stack.isEmpty() == true)
walls[i] = -1;
else
walls[i] = stack.peek();
stack.push(i);
}
return walls;
}
}