-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy path6_MaximalRectangle.cpp
More file actions
133 lines (112 loc) · 3.31 KB
/
6_MaximalRectangle.cpp
File metadata and controls
133 lines (112 loc) · 3.31 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
// For this challenge you will be searching a matrix for the largest rectangle submatrix.
/*
have the function MaximalRectangle(strArr) take the strArr parameter being passed which will be a 2D matrix of 0 and 1's, and determine the area of the largest rectangular submatrix that contains all 1's. For example: if strArr is ["10100", "10111", "11111", "10010"] then this looks like the following matrix:
For the input above, you can see the bolded 1's create the largest rectangular submatrix of size 2x3, so your program should return the area which is 6. You can assume the input will not be empty.
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int getArea(string[], int, int, int);
bool checkArea(string[], int, int, int, int);
/*
One approach is to step through each point
for each point we expand its width and height
we can then check if the possible rectangle is valid
meaning it must be filled with all 1s
if valid we determine the area and update our highest value
*/
int MaximalRectangle(string strArr[], int size)
{
int total = 1;
// traverse for valid points to analyze
for (int row = 0; row < size; row++)
{
for (int col = 0; col < strArr[row].length(); col++)
{
// condition to get a starting point to analyze from
if (strArr[row][col] == '1')
{
int area = getArea(strArr, size, row, col);
// update value
if (area > total)
{
total = area;
}
}
}
}
return total;
}
// get area method
// will analyze current point and find a rectangle by expanding out
int getArea(string arr[], int size, int row, int col)
{
int tempRow = row;
int tempCol = col;
int width = 1;
int height = 1;
bool widthExpansion;
bool heightExpansion;
while (tempRow + 1 < size || tempCol + 1 < arr[0].length())
{
// flags to help determine if either both the width and height were valid in expanding
widthExpansion = false;
heightExpansion = false;
// expand the width or height
if (tempRow + 1 < size && arr[tempRow + 1][col] == '1')
{
tempRow++;
heightExpansion = true;
}
if (tempCol + 1 < arr[0].length() && arr[row][tempCol + 1] == '1')
{
tempCol++;
widthExpansion = true;
}
// check if current expansion is a valid rectangle for both the width and height
if (widthExpansion && heightExpansion && checkArea(arr, row, col, tempRow, tempCol))
{
width++;
height++;
}
else if (widthExpansion && checkArea(arr, row, col, tempRow, tempCol)) // valid width expansion
{
width++;
}
else if (heightExpansion && checkArea(arr, row, col, tempRow, tempCol)) // valid height expansion
{
height++;
}
else
{
return width * height;
}
}
return width * height;
}
// method to check if points provided form a full rectangle
bool checkArea(string arr[], int row, int col, int rowLimit, int colLimit)
{
for (row; row <= rowLimit; row++)
{
for (int y = col; y <= colLimit; y++)
{
if (arr[row][y] != '1')
{
return false;
}
}
}
return true;
}
int main()
{
string A[] = { "10100", "10111", "11111", "10010" };
string B[] = { "1011", "0011", "0111", "1111" };
string C[] = { "101", "111", "001" };
cout << MaximalRectangle(A, sizeof(A) / sizeof(A[0])) << endl; // 6
cout << MaximalRectangle(B, sizeof(B) / sizeof(B[0])) << endl; // 8
cout << MaximalRectangle(C, sizeof(C) / sizeof(C[0])) << endl; // 3
return 0;
}