-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaximumSquareWithAllZeros.cpp
More file actions
100 lines (76 loc) · 1.96 KB
/
maximumSquareWithAllZeros.cpp
File metadata and controls
100 lines (76 loc) · 1.96 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
// Maximum Square Matrix With All Zeros
// Given a n*m matrix which contains only 0s and 1s, find out the size of maximum square sub-matrix with all 0s. You need to return the size of square with all 0s.
// Input format :
// Line 1 : n and m (space separated positive integers)
// Next n lines : m elements of each row (separated by space).
// Output Format:
// Line 1 : Size of maximum square sub-matrix
// Sample Input :
// 3 3
// 1 1 0
// 1 1 1
// 1 1 1
// Sample Output :
// 1
#include<iostream>
using namespace std;
int findMaxSquareWithAllZeros(int** arr, int row, int col){
/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
int **dp = new int*[row];
for(int i = 0; i < row; i++) {
dp[i] = new int[col];
}
for(int i = 0; i < row; i++) {
dp[i][0] = arr[i][0] == 0 ? 1 : 0;
}
for(int i = 0; i < col; i++) {
dp[0][i] = arr[0][i] == 0 ? 1 : 0;
}
for(int i = 1; i < row; i++) {
for(int j = 1; j < col; j++) {
if(arr[i][j] == 1) {
dp[i][j] = 0;
continue;
}
dp[i][j] = 1 + min(dp[i-1][j-1], min(dp[i][j-1], dp[i-1][j]));
}
}
int maxLength = 0;
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
maxLength = dp[i][j] > maxLength ? dp[i][j] : maxLength;
}
}
for(int i = 0; i < row; i++) {
delete [] dp[i];
}
delete dp;
return maxLength;
}
int main()
{
int **arr,n,m,i,j;
cin>>n>>m;
arr=new int*[n];
for(i=0;i<n;i++)
{
arr[i]=new int[m];
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>arr[i][j];
}
}
cout << findMaxSquareWithAllZeros(arr,n,m) << endl;
for(int i = 0; i < n; i++) {
delete [] arr[i];
}
delete arr;
return 0;
}