-
Notifications
You must be signed in to change notification settings - Fork 110
/
solution.java
36 lines (32 loc) · 1.06 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
import java.util.Scanner;
import java.util.Stack;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int[] heights = new int[N + 1];
for (int i = 0; i < N; i++) { //run for loop
heights[i] = in.nextInt();
}
/*
Stack approach
Add into stack when the input value is greater than the top value in the stack
else, pop the stack till the stacks top value is less than or equal
and check the area and add that to stack
final step - empty the stack while calculating the area
*/
long maxArea = 0;
Stack<Integer> indices = new Stack<Integer>();
for (int i = 0; i < heights.length; i++) {
while (!indices.empty() && heights[i] <= heights[indices.peek()]) {
int index = indices.pop(); // if condition holds true then delete one element and assign to index variable
long area = (long) heights[index]
* (i - (indices.empty() ? 0 : (indices.peek() + 1)));
maxArea = Math.max(maxArea, area);
}
indices.push(i);
}
System.out.println(maxArea);
in.close();
}
}