forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxRectangleAreaInHistogram.java
79 lines (69 loc) · 2.91 KB
/
MaxRectangleAreaInHistogram.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.rampatra.stacks;
/**
* Given a bar histogram, calculate the maximum possible rectangle area in the histogram. Consider each bar is 1 unit
* wide.
* <p>
* Level: Hard
* Time Complexity: O(n)
* Space Complexity: O(n)
*
* @author rampatra
* @since 2019-04-04
*/
public class MaxRectangleAreaInHistogram {
public static int getMaxRectangleArea(int[] hist) {
Stack<Integer> stack = new Stack<>();
int area;
int maxArea = 0;
int currMaxIndex;
int i = 0;
while (i < hist.length) {
// keep adding indexes of bars which are equal to or larger than what's there in stack currently
if (stack.isEmpty() || hist[i] >= hist[stack.peek()]) {
stack.push(i);
i++;
} else {
/*
Whenever we encounter a bar smaller than what is there in the stack, we pop the topmost
element and compute the area.
*/
currMaxIndex = stack.pop();
/*
Compute area from stack.peek() to (i - 1),
Why (i - 1)? Because i has been incremented and is now pointing to the next bar in the
histogram
Why stack.peak()? Because hist[stack.peek() + 1] is the last bar in the histogram with height more
than or equal to the current bar height
*/
area = hist[currMaxIndex] * (i - (stack.isEmpty() ? 0 : stack.peek() + 1));
maxArea = Math.max(maxArea, area);
}
}
/*
Process the left over elements in the stack. Note: the below code can also be merged with the loop
above but I have separated it out for simplicity.
*/
while (!stack.isEmpty()) {
currMaxIndex = stack.pop();
area = hist[currMaxIndex] * (i - (stack.isEmpty() ? 0 : stack.peek() + 1));
maxArea = Math.max(maxArea, area);
}
return maxArea;
}
public static void main(String[] args) {
/*
___
___ | | ___
| |__| |__| |
| | | | | |
2 1 3 1 2
Max. area in this histogram is 5, which is the bottom horizontal rectangle.
*/
System.out.println(getMaxRectangleArea(new int[]{2, 1, 3, 1, 2})); // maxArea = 5
System.out.println(getMaxRectangleArea(new int[]{2, 1, 5, 6, 2, 3})); // maxArea = 10
System.out.println(getMaxRectangleArea(new int[]{2, 2, 2, 6, 1, 5, 4, 2, 2, 2, 2})); // maxArea = 12
// edge cases
System.out.println(getMaxRectangleArea(new int[]{})); // maxArea = 0
System.out.println(getMaxRectangleArea(new int[]{1})); // maxArea = 1
}
}