Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 52cbdd9

Browse files
dp: max area
1 parent 2a1038d commit 52cbdd9

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* @lc app=leetcode.cn id=84 lang=cpp
3+
*
4+
* [84] 柱状图中最大的矩形
5+
*
6+
* https://leetcode.cn/problems/largest-rectangle-in-histogram/description/
7+
*
8+
* algorithms
9+
* Hard (44.98%)
10+
* Likes: 2446
11+
* Dislikes: 0
12+
* Total Accepted: 342.6K
13+
* Total Submissions: 761.7K
14+
* Testcase Example: '[2,1,5,6,2,3]'
15+
*
16+
* 给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为
17+
* 1 。
18+
*
19+
* 求在该柱状图中,能够勾勒出来的矩形的最大面积。
20+
*
21+
*
22+
*
23+
* 示例 1:
24+
*
25+
*
26+
*
27+
*
28+
* 输入:heights = [2,1,5,6,2,3]
29+
* 输出:10
30+
* 解释:最大的矩形为图中红色区域,面积为 10
31+
*
32+
*
33+
* 示例 2:
34+
*
35+
*
36+
*
37+
*
38+
* 输入: heights = [2,4]
39+
* 输出: 4
40+
*
41+
*
42+
*
43+
* 提示:
44+
*
45+
*
46+
* 1
47+
* 0
48+
*
49+
*
50+
*/
51+
52+
#include <iostream>
53+
#include <stack>
54+
#include <vector>
55+
using namespace std;
56+
57+
// @lc code=start
58+
class Solution {
59+
public:
60+
// 每个高度对应的最大面积由宽度决定,宽度则为左右第一个比该高度小所包起来的区间
61+
// 举例[8,2,4,5,3,2], 直接看245,到3时则可以确定5的最大面积是5,然后4是4*2
62+
// 2则还不能确定, 所以,发现递增的时候先记录起来,当发现比最新值小,则可以计算
63+
// 显然可以用栈来记录,也就是单调栈
64+
int largestRectangleArea(vector<int> &heights) {
65+
int n = heights.size();
66+
if (n < 2) {
67+
return n < 1 ? 0 : heights[0];
68+
}
69+
stack<int> stk; // 存位置
70+
stk.push(0);
71+
int ret = heights[0];
72+
for (int i = 1; i < heights.size(); i++) {
73+
while (!stk.empty() && heights[i] < heights[stk.top()]) {
74+
int h = heights[stk.top()];
75+
stk.pop();
76+
// 左侧是栈的下一个元素,右侧是i,栈空了说明左侧没有更小,也就左为0,宽度i-0,比如5/4/1/2的5和4
77+
int w = stk.empty() ? i : (i - stk.top() - 1);
78+
ret = max(ret, w * h);
79+
}
80+
stk.push(i);
81+
}
82+
while (!stk.empty()) {
83+
int h = heights[stk.top()];
84+
stk.pop();
85+
int w = stk.empty() ? n : (n - stk.top() - 1);
86+
ret = max(ret, w * h);
87+
}
88+
return ret;
89+
}
90+
};
91+
// @lc code=end
92+
93+
int main(int argc, const char **argv) {
94+
Solution s;
95+
vector<int> arr({5, 4, 1, 2});
96+
auto ret = s.largestRectangleArea(arr);
97+
std::cout << "ret: " << ret;
98+
return 0;
99+
}

‎cpp/leetcode/85.最大矩形.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* @lc app=leetcode.cn id=85 lang=cpp
3+
*
4+
* [85] 最大矩形
5+
*
6+
* https://leetcode.cn/problems/maximal-rectangle/description/
7+
*
8+
* algorithms
9+
* Hard (54.65%)
10+
* Likes: 1528
11+
* Dislikes: 0
12+
* Total Accepted: 175.6K
13+
* Total Submissions: 321.4K
14+
* Testcase Example:
15+
* '[["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]'
16+
*
17+
* 给定一个仅包含 0 和 1 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1
18+
* 的最大矩形,并返回其面积。
19+
*
20+
*
21+
*
22+
* 示例 1:
23+
*
24+
*
25+
* 输入:matrix =
26+
* [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
27+
* 输出:6
28+
* 解释:最大矩形如上图所示。
29+
*
30+
*
31+
* 示例 2:
32+
*
33+
*
34+
* 输入:matrix = []
35+
* 输出:0
36+
*
37+
*
38+
* 示例 3:
39+
*
40+
*
41+
* 输入:matrix = [["0"]]
42+
* 输出:0
43+
*
44+
*
45+
* 示例 4:
46+
*
47+
*
48+
* 输入:matrix = [["1"]]
49+
* 输出:1
50+
*
51+
*
52+
* 示例 5:
53+
*
54+
*
55+
* 输入:matrix = [["0","0"]]
56+
* 输出:0
57+
*
58+
*
59+
*
60+
*
61+
* 提示:
62+
*
63+
*
64+
* rows == matrix.length
65+
* cols == matrix[0].length
66+
* 1 <= row, cols <= 200
67+
* matrix[i][j] 为 '0' 或 '1'
68+
*
69+
*
70+
*/
71+
#include <iostream>
72+
#include <stack>
73+
#include <vector>
74+
using namespace std;
75+
// @lc code=start
76+
class Solution {
77+
public:
78+
// 可以看成是84题的2维版本,0-n行的最大面积
79+
int maximalRectangle(vector<vector<char>> &matrix) {
80+
int m = matrix.size();
81+
if (m == 0) {
82+
return 0;
83+
}
84+
int n = matrix[0].size();
85+
vector<vector<int>> heights(m, vector<int>(n, 0));
86+
87+
for (int i = 0; i < m; i++) {
88+
for (int j = 0; j < n; j++) {
89+
if (matrix[i][j] == '1') {
90+
heights[i][j] = (i == 0 ? 0 : heights[i - 1][j]) + 1;
91+
}
92+
}
93+
}
94+
95+
int ret = 0;
96+
// 每一层按84题算一次
97+
for (int i = 0; i < m; i++) {
98+
ret = max(ret, largestRectangleArea(heights[i]));
99+
}
100+
return ret;
101+
}
102+
103+
private:
104+
// 84题的最大面积
105+
int largestRectangleArea(vector<int> &heights) {
106+
int n = heights.size();
107+
if (n < 2) {
108+
return n < 1 ? 0 : heights[0];
109+
}
110+
stack<int> stk; // 存位置
111+
stk.push(0);
112+
int ret = heights[0];
113+
for (int i = 1; i < heights.size(); i++) {
114+
while (!stk.empty() && heights[i] < heights[stk.top()]) {
115+
int h = heights[stk.top()];
116+
stk.pop();
117+
// 左侧是栈的下一个元素,右侧是i,栈空了说明左侧没有更小,也就左为0,宽度i-0,比如5/4/1/2的5和4
118+
int w = stk.empty() ? i : (i - stk.top() - 1);
119+
ret = max(ret, w * h);
120+
}
121+
stk.push(i);
122+
}
123+
while (!stk.empty()) {
124+
int h = heights[stk.top()];
125+
stk.pop();
126+
int w = stk.empty() ? n : (n - stk.top() - 1);
127+
ret = max(ret, w * h);
128+
}
129+
return ret;
130+
}
131+
};
132+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /