-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMaximalSquare.py
More file actions
22 lines (22 loc) · 791 Bytes
/
MaximalSquare.py
File metadata and controls
22 lines (22 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution(object):
def maximalSquare(self, matrix):
n = len(matrix)
if n==0: return 0
m = len(matrix[0])
s = [[0]*(m+1) for i in xrange(n+1)]
for i in xrange(1,n+1):
for j in xrange(1,m+1):
s[i][j] = s[i-1][j]+s[i][j-1]-s[i-1][j-1]+int(matrix[i-1][j-1])
def square(i,j,L):
return s[i+L-1][j+L-1]-s[i-1][j+L-1]-s[i+L-1][j-1]+s[i-1][j-1] == L*L
def check(L):
for i in xrange(1,n+1):
for j in xrange(1,m+1):
if i+L-1<=n and j+L-1<=m and square(i,j,L): return True
return False
l,r = 0,max(n,m)+1
while l+1<r:
mid = (l+r)/2
if check(mid): l=mid
else: r = mid
return l*l