-
Notifications
You must be signed in to change notification settings - Fork 43
/
LargestRectangleInHistogram.java
125 lines (113 loc) · 4.39 KB
/
LargestRectangleInHistogram.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package LeetCodeJava.Stack;
// https://leetcode.com/problems/largest-rectangle-in-histogram/
import java.util.ArrayDeque;
import java.util.Deque;
public class LargestRectangleInHistogram {
// V0
// IDEA : STACK
// https://www.bilibili.com/list/525438321?sort_field=pubtime&spm_id_from=333.999.0.0&oid=992760704&bvid=BV1Ns4y1o7uB
// public int largestRectangleArea(int[] heights) {
//
// return 0;
// }
// V0'
// IDEA : STACK
// https://leetcode.com/problems/largest-rectangle-in-histogram/editorial/
public int largestRectangleArea_(int[] heights) {
Deque<Integer> stack = new ArrayDeque<>();
stack.push(-1);
int length = heights.length;
int maxArea = 0;
for (int i = 0; i < length; i++) {
while ((stack.peek() != -1)
&& (heights[stack.peek()] >= heights[i])) {
int currentHeight = heights[stack.pop()];
int currentWidth = i - stack.peek() - 1;
maxArea = Math.max(maxArea, currentHeight * currentWidth);
}
stack.push(i);
}
while (stack.peek() != -1) {
int currentHeight = heights[stack.pop()];
int currentWidth = length - stack.peek() - 1;
maxArea = Math.max(maxArea, currentHeight * currentWidth);
}
return maxArea;
}
// V1
// IDEA : BRUTE FORCE
// https://leetcode.com/problems/largest-rectangle-in-histogram/editorial/
public int largestRectangleArea_2(int[] heights) {
int maxarea = 0;
for (int i = 0; i < heights.length; i++) {
for (int j = i; j < heights.length; j++) {
int minheight = Integer.MAX_VALUE;
for (int k = i; k <= j; k++)
minheight = Math.min(minheight, heights[k]);
maxarea = Math.max(maxarea, minheight * (j - i + 1));
}
}
return maxarea;
}
// V2
// IDEA : better BRUTE FORCE
// https://leetcode.com/problems/largest-rectangle-in-histogram/editorial/
public int largestRectangleArea_3(int[] heights) {
int maxArea = 0;
int length = heights.length;
for (int i = 0; i < length; i++) {
int minHeight = Integer.MAX_VALUE;
for (int j = i; j < length; j++) {
minHeight = Math.min(minHeight, heights[j]);
maxArea = Math.max(maxArea, minHeight * (j - i + 1));
}
}
return maxArea;
}
// V3
// IDEA : Divide and Conquer Approach
// https://leetcode.com/problems/largest-rectangle-in-histogram/editorial/
public int calculateArea(int[] heights, int start, int end) {
if (start > end)
return 0;
int minindex = start;
for (int i = start; i <= end; i++)
if (heights[minindex] > heights[i])
minindex = i;
return Math.max(heights[minindex] * (end - start + 1),
Math.max(calculateArea(heights, start, minindex - 1),
calculateArea(heights, minindex + 1, end))
);
}
public int largestRectangleArea_4(int[] heights) {
return calculateArea(heights, 0, heights.length - 1);
}
// V4
// IDEA : Better Divide and Conquer
// https://leetcode.com/problems/largest-rectangle-in-histogram/editorial/
// https://leetcode.com/problems/largest-rectangle-in-histogram/solutions/28941/segment-tree-solution-just-another-idea-onlogn-solution/
// V5
// IDEA : STACK
// https://leetcode.com/problems/largest-rectangle-in-histogram/editorial/
public int largestRectangleArea_6(int[] heights) {
Deque<Integer> stack = new ArrayDeque<>();
stack.push(-1);
int length = heights.length;
int maxArea = 0;
for (int i = 0; i < length; i++) {
while ((stack.peek() != -1)
&& (heights[stack.peek()] >= heights[i])) {
int currentHeight = heights[stack.pop()];
int currentWidth = i - stack.peek() - 1;
maxArea = Math.max(maxArea, currentHeight * currentWidth);
}
stack.push(i);
}
while (stack.peek() != -1) {
int currentHeight = heights[stack.pop()];
int currentWidth = length - stack.peek() - 1;
maxArea = Math.max(maxArea, currentHeight * currentWidth);
}
return maxArea;
}
}