Skip to content

Commit c25c12e

Browse files
authored
Create largest-1-bordered-square.py
1 parent b7a03e6 commit c25c12e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Python/largest-1-bordered-square.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(n^3)
2+
# Space: O(n^2)
3+
4+
class Solution(object):
5+
def largest1BorderedSquare(self, grid):
6+
"""
7+
:type grid: List[List[int]]
8+
:rtype: int
9+
"""
10+
top, left = [a[:] for a in grid], [a[:] for a in grid]
11+
for i in xrange(len(grid)):
12+
for j in xrange(len(grid[0])):
13+
if not grid[i][j]:
14+
continue
15+
if i:
16+
top[i][j] = top[i-1][j] + 1
17+
if j:
18+
left[i][j] = left[i][j-1] + 1
19+
for l in reversed(xrange(1, min(len(grid), len(grid[0]))+1)):
20+
for i in xrange(len(grid)-l+1):
21+
for j in xrange(len(grid[0])-l+1):
22+
if min(top[i+l-1][j],
23+
top[i+l-1][j+l-1],
24+
left[i][j+l-1],
25+
left[i+l-1][j+l-1]) >= l:
26+
return l*l
27+
return 0

0 commit comments

Comments
 (0)