-
Notifications
You must be signed in to change notification settings - Fork 138
/
Solution.java
51 lines (45 loc) · 1.24 KB
/
Solution.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
49
50
51
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Stack;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] h = new int[n];
for(int h_i = 0; h_i < n; h_i++){
h[h_i] = in.nextInt();
}
int maxArea = Integer.MIN_VALUE;
Stack<Integer> stack = new Stack<Integer>();
int i = 0;
int removedIndex;
for (; i < n; i++) {
if (stack.empty() || h[stack.peek()] <= h[i]) {
stack.push(i);
} else {
while (!stack.empty() && h[stack.peek()] > h[i]) {
removedIndex = stack.pop();
if (stack.empty()) {
maxArea = Math.max(h[removedIndex] * i, maxArea);
} else {
maxArea = Math.max(h[removedIndex] * (i - 1 - stack.peek()), maxArea);
}
}
stack.push(i);
}
}
while (!stack.empty()) {
removedIndex = stack.pop();
if (stack.empty()) {
maxArea = Math.max(h[removedIndex] * i, maxArea);
} else {
maxArea = Math.max(h[removedIndex] * (i - 1 - stack.peek()), maxArea);
}
}
System.out.println(maxArea);
in.close();
}
}