-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathLargestRectangle.java
More file actions
51 lines (46 loc) · 1.42 KB
/
LargestRectangle.java
File metadata and controls
51 lines (46 loc) · 1.42 KB
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
49
50
51
/*
Use two stacks, one for height, one for index. if the current height is larger than the peek of height stack, push it in
update the index stack as well, if it is equal, skip. if it is smaller, pop the stack, calculate the current max, push the
correct height and index in (current height and last pop out index)
Don't forget the calculate the height after the loop
*/
import java.util.Stack;
public class Solution {
public int largestRectangleArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
if(height.length == 0) return 0;
if(height.length == 1) return height[0];
Stack<Integer> hs = new Stack<Integer>();
Stack<Integer> inds = new Stack<Integer>();
hs.push(height[0]);
inds.push(0);
int localmax = 0;
int max = 0;
int lastindex = 0;
int lasth = 0;
for(int i=1; i<height.length; i++){
if(height[i]>hs.peek()){
hs.push(height[i]);
inds.push(i);
}
else if(height[i]<hs.peek()){
while(!hs.isEmpty() && hs.peek()>height[i]){
lasth = hs.pop();
lastindex = inds.pop();
localmax = (i-lastindex)*lasth;
max = max > localmax ? max : localmax;
}
hs.push(height[i]);
inds.push(lastindex);
}
}
while(!hs.isEmpty()){
lastindex = inds.pop();
lasth = hs.pop();
localmax = (height.length-lastindex)*lasth;
max = max > localmax ? max :localmax;
}
return max;
}
}