# 【LeetCode】85. Maximal Rectangle【未完待续】

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.

Example:

Input:
[
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]
Output: 6

class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if (!matrix.size() || !matrix[0].size()) {
return 0;
}
for (int i = 0; i < matrix.size(); ++ i) {
matrix[i][0] -= '0';
for(int j = 1; j < matrix[0].size(); ++ j) {
matrix[i][j] = matrix[i][j] - '0' + matrix[i][j - 1];
}
}
int result = 0;
for (int i = 0; i < matrix[0].size(); ++ i) {
for (int j = i; j < matrix[0].size(); ++ j) {
caculate(matrix, i, j, result);
}
}
return result;
}
void caculate(const vector<vector<char>>& matrix, int col_begin, int col_end, int &result) {
int temp = 0, count = col_end - col_begin + 1;
for (int i = 0; i < matrix.size(); ++ i) {
int last_sum = 0;
if (col_begin) {
last_sum = matrix[i][col_begin - 1];
}
temp += count;
int dis = matrix[i][col_end] - last_sum;
if (dis != count) {
temp = 0;
}
result = max(result, temp);
}
}
};

05-23 1544

01-28 2060
07-31 188
09-19 1万+
05-05 234
09-25 1079
11-25 610