Skip to content

Commit 4ac4038

Browse files
authored
Create count-square-submatrices-with-all-ones.py
1 parent 306830e commit 4ac4038

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Time: O(m * n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def countSquares(self, matrix):
6+
"""
7+
:type matrix: List[List[int]]
8+
:rtype: int
9+
"""
10+
for i in xrange(1, len(matrix)):
11+
for j in xrange(1, len(matrix[0])):
12+
if not matrix[i][j]:
13+
continue
14+
l = min(matrix[i-1][j], matrix[i][j-1])
15+
matrix[i][j] = l+1 if matrix[i-l][j-l] else l
16+
return sum(x for row in matrix for x in row)

0 commit comments

Comments
 (0)