-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestRecInDist.java
More file actions
36 lines (32 loc) · 1.05 KB
/
LargestRecInDist.java
File metadata and controls
36 lines (32 loc) · 1.05 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
public class LargestRecInDist {
public int largestRectangleArea(int[] h) {
int max = 0;
Stack<Integer> ss = new Stack<Integer>();
int[] arr = Arrays.copyOf(h, h.length +1);
for(int i = 0; i < arr.length; i++){
while(!ss.isEmpty() && arr[ss.peek()] > arr[i]){
max = Math.max(max, arr[ss.pop()] * (ss.isEmpty()? i: i -ss.peek() -1));
}
ss.push(i);
}
return max;
}
/*
public int largestRectangleArea(int[] hist) {
int len = hist.length;
int i,j,k;
int maxSum=0,prev=-1;
for(i=0;i<len;i++){
if( prev == hist[i] ) continue;
for(j=i-1;j>=0;j--)
if(hist[j]<hist[i]) break;
for(k=i+1;k<len;k++)
if(hist[k]<hist[i]) break;
//sum = height[i]+height[i]*(i-1-j)+height[i]*(k-(i+1)); sum = height[i]*(k-j-1)
maxSum = Math.max( hist[i]*(k-j-1), maxSum);
prev = hist[i];
}
return maxSum;
}
*/
}