forked from hrsvrdhn/DP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxHistogramArea.java
More file actions
36 lines (33 loc) · 939 Bytes
/
MaxHistogramArea.java
File metadata and controls
36 lines (33 loc) · 939 Bytes
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.*;
import java.io.*;
class MaxHistogramArea {
public void solveMaxHistogramArea(int[] input) {
Stack<Integer> stck = new Stack<>();
int maxArea = 0, area;
for(int i=0; i<input.length; i++) {
while(stck.isEmpty() == false && input[stck.peek()] > input[i]) {
int top = stck.pop();
if(stck.isEmpty())
area = input[top] * i;
else
area = input[top] * (i - stck.peek() - 1);
maxArea = Math.max(maxArea, area);
}
stck.push(i);
}
while(stck.isEmpty() == false) {
int top = stck.pop();
if(stck.isEmpty())
area = input[top] * input.length;
else
area = input[top] * (input.length - stck.peek() - 1);
maxArea = Math.max(maxArea, area);
}
System.out.println("Answer = " + maxArea);
}
public static void main(String args[]) {
int input[] = {1, 4, 2, 3, 3};
MaxHistogramArea obj = new MaxHistogramArea();
obj.solveMaxHistogramArea(input);
}
}