forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxSquareMatrixWithOnes.cpp
71 lines (65 loc) · 2.8 KB
/
maxSquareMatrixWithOnes.cpp
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
#include <bits/stdc++.h>
using namespace std;
int maxSizeSquareWithOnes(int** input, int row, int col, int i, int j, int* ans, int** output){
if(i == 0 || j == 0){
output[i][j] = input[i][j];
return input[i][j];
}
if(i < 0 || j < 0)
return INT_MAX;
if(output[i][j] > -1)
return output[i][j];
int option1 = maxSizeSquareWithOnes(input, row, col, i-1, j, ans, output);
int option2 = maxSizeSquareWithOnes(input, row, col, i-1, j-1, ans, output);
int option3 = maxSizeSquareWithOnes(input, row, col, i, j-1, ans, output);
int res = min(option1, min(option2, option3));
if(input[i][j] == 1)
res++;
if(input[i][j] == 0)
res = 0;
*ans = max(*ans, res);
output[i][j] = res;
return res;
}
int main()
{
int row,col;
cout << "Enter number of rows and colums: " << endl;
cin >> row >> col;
int** input = new int*[row];
int** output = new int*[row];
cout << "Enter elements for input matrix: " << endl;
for(int i = 0; i < row; i++){
input[i] = new int[col];
output[i] = new int[col];
for(int j = 0; j < col; j++){
cin >> input[i][j];
output[i][j] = -1;
}
}
int ans = INT_MIN;
maxSizeSquareWithOnes(input, row, col, row-1, col-1, &ans, output);
cout << "Size of Maximum Square Matrix with all 1s is: " << ans << endl;
for(int i = 0; i < row; i++){
delete[] input[i];
delete[] output[i];
}
delete[] input;
delete[] output;
return 0;
}
/*
Sample input/output:
Enter number of rows and colums:
6 5
Enter elements for input matrix:
0 1 1 0 1
1 1 0 1 0
0 1 1 1 0
1 1 1 1 0
1 1 1 1 1
0 0 0 0 0
Size of Maximum Square Matrix with all 1s is: 3
Time complexity = O(rows * cols)
Space complexity = O(rows * cols)
*/