forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maximum_Square_Matrix.py
47 lines (38 loc) · 1002 Bytes
/
Maximum_Square_Matrix.py
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
# find the size of Maximum Square Matrix With All 1s
def MaxSquare(mat):
# creating a null matrix
ans = 0
temp = [[0] * len(mat[0])] * len(mat)
# Traversing through the given matrix
for i in range(len(mat)):
for j in range(len(mat[0])):
temp[i][j] = mat[i][j]
# checking if it is not a corner case:
if i > 0 and j > 0 and mat[i][j] == 1:
temp[i][j] = min(temp[i][j - 1], temp[i - 1][j])
temp[i][j] = min(temp[i][j], temp[i - 1][j - 1]) + 1
# for finding max size of square mat
if ans < temp[i][j]:
ans = temp[i][j]
return ans
mat = []
rows = input("Enter rows")
col = input("Enter col")
for i in range(0, int(rows)):
mat.append([int(j) for j in input().split()])
print("output is", MaxSquare(mat))
'''
output:
Enter rows3
Enter col3
0 0 0
0 1 0
0 0 0
output is 1
Enter rows3
Enter col3
0 1 1
0 1 1
0 0 0
output is 2
Time Complexity : O(rows*col)'''