-
Notifications
You must be signed in to change notification settings - Fork 1
/
221.MaximalSquare.py
executable file
·59 lines (49 loc) · 1.78 KB
/
221.MaximalSquare.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
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 27 18:34:22 2020
@author: nenad
"""
"""
Problem URL: https://leetcode.com/problems/maximal-square/
Problem description:
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
Example:
Input:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Output: 4
"""
# Time: O(m*n), space: O(m*n)
class Solution:
def maximalSquare(self, matrix) -> int:
m = len(matrix)
if m:
n = len(matrix[0])
else:
return 0
sizeSquare = [[0 for j in range(n)] for i in range(m)]
maxSurface = 0
for i in range(m):
for j in range(n):
# sizeSquare[i][j] represents size of the square - edge length
# where i,j position is the right bottom position -> surface of that square
# is sizeSquare[i][j] ^ 2
if i == 0 or j == 0:
sizeSquare[i][j] = int(matrix[i][j])
else:
if matrix[i][j] == "1":
# try to expand square above, left and diagonal
# pick the smallest value - reason: e.g. we can have 0 in some of
# elements (top, left, diagonal) so in that case this square cannot be made over that value
sizeSquare[i][j] = min(sizeSquare[i-1][j], sizeSquare[i][j-1],\
sizeSquare[i-1][j-1]) + 1
else:
sizeSquare[i][j] = 0
maxSurface = max(maxSurface, sizeSquare[i][j] ** 2)
return maxSurface
sol = Solution()
# Test 1
matrix = ["1 0 1 0 0".split(), "1 0 1 1 1".split(), "1 1 1 1 1".split(), "1 0 0 1 0".split()]