-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
58 lines (48 loc) · 1.87 KB
/
code.cpp
File metadata and controls
58 lines (48 loc) · 1.87 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
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
// Find the dimensions
int m = matrix.size();
int n = matrix[0].size();
// Form the matrix to track consecutive 1's row and column wise
vector<vector<vector<int>>> consecutiveOnes(m, vector<vector<int>>(n, vector<int>(2, 0)));
for (int i=m-1; i>=0; i--) {
for (int j=n-1; j>=0; j--) {
if (matrix[i][j] == '1') {
// Increase the consecutive ones count in that row
consecutiveOnes[i][j][0] = 1 + ( j == n-1 ? 0 : consecutiveOnes[i][j+1][0]);
// Increase the consecutive ones count in that column
consecutiveOnes[i][j][1] = 1 + ( i == m-1 ? 0 : consecutiveOnes[i+1][j][1]);
}
}
}
// Find the area of the maximal rectangle
int maximalRectangleArea = 0;
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
// Single row/column rectangles
maximalRectangleArea = max(maximalRectangleArea, consecutiveOnes[i][j][0]);
maximalRectangleArea = max(maximalRectangleArea, consecutiveOnes[i][j][1]);
// Parse and find the maximum rectangle possible
// Check for columns
int numColumns = consecutiveOnes[i][j][0];
int numRows = consecutiveOnes[i][j][1];
for (int k=j; k<j+numColumns; k++) {
numRows = min(numRows, consecutiveOnes[i][k][1]);
// Update the maximal rectangle area
maximalRectangleArea = max(maximalRectangleArea, (numRows * (k-j+1)));
}
// Check for rows
numColumns = consecutiveOnes[i][j][0];
numRows = consecutiveOnes[i][j][1];
for (int k=i; k<i+numRows; k++) {
numColumns = min(numColumns, consecutiveOnes[k][j][0]);
// Update the maximal rectangle area
maximalRectangleArea = max(maximalRectangleArea, ((k-i+1) * numColumns));
}
}
}
// Return the maximum rectangle's area
return maximalRectangleArea;
}
};