forked from prakhar1989/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_subsquare_matrix.py
More file actions
30 lines (28 loc) · 967 Bytes
/
max_subsquare_matrix.py
File metadata and controls
30 lines (28 loc) · 967 Bytes
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
""" Given a binary matrix, find out the maximum size square sub-matrix with all 1s.
For example, consider the below binary 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
The maximum square sub-matrix with all set bits is
1 1 1
1 1 1
1 1 1
"""
from copy import deepcopy
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]]
def find_sub_matrix_size(matrix):
copy_matrix = deepcopy(matrix)
for i in range(1, len(matrix)):
for j in range(1, len(matrix[0])):
if matrix[i][j] == 1:
copy_matrix[i][j] = min(copy_matrix[i-1][j],
copy_matrix[i][j-1],
copy_matrix[i-1][j-1]) + 1
else:
copy_matrix[i][j] = 0
return max([item for rows in copy_matrix for item in rows])
print find_sub_matrix_size(matrix)