# 最大矩形

给定一个仅包含 01 、大小为 rows x cols 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

示例 1:

输入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
输出:
6
解释:
最大矩形如上图所示。

示例 2:

输入:matrix = []
输出:
0

示例 3:

输入:matrix = [["0"]]
输出:
0

示例 4:

输入:matrix = [["1"]]
输出:
1

示例 5:

输入:matrix = [["0","0"]]
输出:
0

提示:

以下错误的选项是?

## aop ### before ```c #include using namespace std; ``` ### after ```c int main() { Solution sol; vector> matrix = {{'1', '0', '1', '0', '0'}, {'1', '0', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '0', '0', '1', '0'}}; int res; res = sol.maximalRectangle(matrix); cout << res << endl; return 0; } ``` ## 答案 ```c class Solution { public: int largestRectangleArea(vector &heights) { stack h; heights.push_back(0); int ans = 0, hsize = heights.size(); for (int i = 0; i < hsize; i++) { while (!h.empty() && heights[h.top()] > heights[i]) { int top = h.top(); h.pop(); ans = max(ans, heights[top] * (h.empty() ? i : (i - h.top()))); } h.push(i); } return ans; } int maximalRectangle(vector> &matrix) { if (matrix.empty()) return 0; int n = matrix.size(), m = matrix[0].size(), ans = 0; vector> num(n, vector(m, 0)); for (int j = 0; j < m; j++) { num[0][j] = (matrix[0][j] == '0') ? 0 : 1; for (int i = 1; i < n; i++) num[i][j] = (matrix[i][j] == '0') ? 0 : num[i - 1][j] + 1; } for (int i = 0; i < n; i++) { int area = largestRectangleArea(num[i]); ans = max(ans, area); } return ans; } }; ``` ## 选项 ### A ```c class Solution { public: int maximalRectangle(vector> &matrix) { int res = 0; vector height; for (int i = 0; i < matrix.size(); ++i) { height.resize(matrix[i].size()); for (int j = 0; j < matrix[i].size(); ++j) { height[j] = matrix[i][j] == '0' ? 0 : (1 + height[j]); } res = max(res, largestRectangleArea(height)); } return res; } int largestRectangleArea(vector &heights) { if (heights.empty()) return 0; stack st; heights.push_back(0); int res0 = 0; for (int i = 0; i < heights.size(); i++) { while (!st.empty() && heights[i] < heights[st.top()]) { int curHeight = heights[st.top()]; st.pop(); int width = st.empty() ? i : i - st.top() - 1; if (width * curHeight > res0) res0 = width * curHeight; } st.push(i); } return res0; } }; ``` ### B ```c class Solution { public: int maximalRectangle(vector> &matrix) { if (matrix.size() == 0) { return 0; } int m = matrix.size(); int n = matrix[0].size(); int left[n]; int right[n]; int height[n]; memset(right, n, sizeof(right)); memset(left, n, sizeof(left)); memset(height, n, sizeof(height)); int maxarea = 0; for (int i = 0; i < m; ++i) { int cur_left = 0, cur_right = n; for (int j = 0; j < n; ++j) { if (matrix[i][j] == '1') { height[j]++; } else { height[j] = 0; } } for (int j = 0; j < n; ++j) { if (matrix[i][j] == '1') { left[j] = max(left[j], cur_left); } else { left[j] = 0, cur_left = j + 1; } } for (int j = n - 1; j >= 0; --j) { if (matrix[i][j] == '1') { right[j] = min(right[j], cur_right); } else { right[j] = n; cur_right = j; } } for (int j = 0; j < n; ++j) { maxarea = max(maxarea, (right[j] - left[j]) * height[j]); } } return maxarea; } }; ``` ### C ```c class Solution { public: int maximalRectangle(vector> &matrix) { if (matrix.size() == 0) { return 0; } int maxarea = 0; int dp[matrix.size()][matrix[0].size()]; memset(dp, 0, sizeof(dp)); for (int i = 0; i < matrix.size(); ++i) { for (int j = 0; j < matrix[0].size(); ++j) { if (matrix[i][j] == '1') { dp[i][j] = j == 0 ? 1 : dp[i][j - 1] + 1; int width = dp[i][j]; for (int k = i; k >= 0; k--) { width = min(width, dp[k][j]); maxarea = max(maxarea, width * (i - k + 1)); } } } } return maxarea; } }; ```