-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_84LargestRectangle.java
46 lines (40 loc) · 1.45 KB
/
_84LargestRectangle.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
package com.test.leetcode;
class _84LargestRectangle {
public static int largestRectangleArea(int[] heights) {
int[][] stack = new int[heights.length][2];
int top = 0, max = 0;
for (int i=0; i<heights.length; i++) {
if (top==0 || heights[i]>stack[top-1][0]) { //second one taller than first one.
stack[top][0] = heights[i]; stack[top][1] = i;
top++;
max = Math.max(max, heights[i]);
} else {
int j = top-1;
while (top>0 && stack[top-1][0]>=heights[i]) {
j=stack[top-1][1];
max = Math.max(max, stack[top-1][0]*(i-j));
top--;
}
stack[top][0] = heights[i]; stack[top][1] = j; top++;
max = Math.max(max, heights[i]*(i-j+1));
//System.out.println(j);
}
//for (int x = 0; x < top; x++) System.out.println(stack[x][0]+" "+stack[x][1]);
//System.out.println("i="+i+" max="+max+" top="+top);
}
if (top>0) {
int len = heights.length-1;
while (top>0) {
max = Math.max(max, stack[top-1][0]*(len-stack[top-1][1]+1));
//System.out.println(stack[top-1][0]+" "+(len-stack[top-1][1]+1));
top--;
}
}
return max;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] heights = {2,1,5,4,2,3,1,2,3,2};
System.out.println(largestRectangleArea(heights));
}
}