-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestRectangle.java
48 lines (40 loc) · 1.28 KB
/
LargestRectangle.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
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
/**
* Created by Bryan Tamada on 12/12/15.
*/
public class LargestRectangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack<Integer> stack = new Stack<>();
int N = sc.nextInt();
int[] hist = new int[N];
for(int i = 0; i < N; ++i) {
hist[i] = sc.nextInt();
}
System.out.println(getMaxArea(hist,N));
}
private static int getMaxArea(int[] hist, int n) {
Stack<Integer> stack = new Stack<>();
int max_area = 0;
int tp = 0;
int area_with_top = 0;
int i = 0;
while(i < n) {
if(stack.empty() || hist[stack.peek()] <= hist[i]){
stack.push(i++);
} else {
tp = stack.pop();
area_with_top = hist[tp] * (stack.empty() ? i : i - stack.peek() - 1);
if(max_area < area_with_top) max_area = area_with_top;
}
}
while(!stack.empty()){
tp = stack.pop();
area_with_top = hist[tp] * (stack.empty() ? i : i - stack.peek() - 1);
if(max_area < area_with_top) max_area = area_with_top;
}
return max_area;
}
}