forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestrec.java
More file actions
executable file
·38 lines (34 loc) · 1.01 KB
/
largestrec.java
File metadata and controls
executable file
·38 lines (34 loc) · 1.01 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
public class Solution {
class Ele{
public int h;
public int i;
Ele(int h, int i){
this.h = h;
this.i = i;
}
}
public int largestRectangleArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
Stack<Ele> k = new Stack<Ele>();
int max = 0;
for(int i=0;i<height.length;i++){
if(k.isEmpty() || height[i] > k.peek().h ){
k.push(new Ele(height[i],i));
}else{
int prev = i;
while(!k.isEmpty() && k.peek().h > height[i]){
Ele e = k.pop();
prev = e.i;
max = Math.max(max,(i-e.i)*e.h);
}
k.push(new Ele(height[i],prev));
}
}
while(!k.isEmpty()){
Ele e = k.pop();
max = Math.max(max,(height.length-e.i)*e.h);
}
return max;
}
}