-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLeetCode84.java
More file actions
139 lines (130 loc) · 3.95 KB
/
LeetCode84.java
File metadata and controls
139 lines (130 loc) · 3.95 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package problems;
import java.util.ArrayDeque;
import java.util.Deque;
public class LeetCode84 {
/**
* 暴力解法
* @param heights
* @return
*/
public int largestRectangleArea(int[] heights) {
int n = heights.length;
int ans = 0;
// 枚举左边界
for (int left = 0; left < n; left++) {
int minHeight = Integer.MAX_VALUE;
// 枚举右边界
for (int right = left; right < n; right++) {
// 确定高度
minHeight = Math.min(minHeight, heights[right]);
// 计算面积
ans = Math.max(ans, (right - left + 1) * minHeight);
}
}
return ans;
}
/**
* 优化
* @param heights
* @return
*/
public int largestRectangleArea1(int[] heights) {
int n = heights.length;
int ans = 0;
for (int mid = 0; mid < n; mid++) {
// 枚举高
int height = heights[mid];
int left = mid, right = mid;
// 确定左右边界
while (left - 1 >= 0 && heights[left - 1] >= height) {
--left;
}
while (right + 1 < n && heights[right + 1] >= height) {
++right;
}
// 计算面积
ans = Math.max(ans, (right - left + 1) * height);
}
return ans;
}
/**
* 单调栈
* @param heights
* @return
*/
public int largestRectangleArea2(int[] heights) {
int len = heights.length;
if (len == 0) {
return 0;
}
if (len == 1) {
return heights[0];
}
int res = 0;
Deque<Integer> stack = new ArrayDeque<>(len);
for (int i = 0; i < len; i++) {
// 这个 while 很关键,因为有可能不止一个柱形的最大宽度可以被计算出来
while (!stack.isEmpty() && heights[i] < heights[stack.peekLast()]) {
int curHeight = heights[stack.pollLast()];
while (!stack.isEmpty() && heights[stack.peekLast()] == curHeight) {
stack.pollLast();
}
int curWidth;
if (stack.isEmpty()) {
curWidth = i;
} else {
curWidth = i - stack.peekLast() - 1;
}
// System.out.println("curIndex = " + curIndex + " " + curHeight * curWidth);
res = Math.max(res, curHeight * curWidth);
}
stack.addLast(i);
}
while (!stack.isEmpty()) {
int curHeight = heights[stack.pollLast()];
while (!stack.isEmpty() && heights[stack.peekLast()] == curHeight) {
stack.pollLast();
}
int curWidth;
if (stack.isEmpty()) {
curWidth = len;
} else {
curWidth = len - stack.peekLast() - 1;
}
res = Math.max(res, curHeight * curWidth);
}
return res;
}
/**
* 单调栈 + 哨兵
* @param heights
* @return
*/
public int largestRectangleArea3(int[] heights) {
int len = heights.length;
if(len == 0) {
return 0;
}
if(len == 1){
return heights[0];
}
int area = 0;
int[] newHeights = new int[len + 2];
for (int i = 0; i < len; i++) {
newHeights[i + 1] = heights[i];
}
len += 2;
heights = newHeights;
Deque<Integer> stack = new ArrayDeque<>();
stack.addLast(0);
for (int i = 1; i < len; i++) {
while (heights[stack.peekLast()] > heights[i]) {
int height = heights[stack.removeLast()];
int width = i - stack.peekLast() - 1;
area = Math.max(area, width * height);
}
stack.addLast(i);
}
return area;
}
}