代码拉取完成,页面将自动刷新
# Time: O(n^2)
# Space: O(n^2)
class Solution(object):
def largestIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
def dfs(r, c, index, grid):
if not (0 <= r < len(grid) and
0 <= c < len(grid[0]) and
grid[r][c] == 1):
return 0
result = 1
grid[r][c] = index
for d in directions:
result += dfs(r+d[0], c+d[1], index, grid)
return result
area = {}
index = 2
for r in xrange(len(grid)):
for c in xrange(len(grid[r])):
if grid[r][c] == 1:
area[index] = dfs(r, c, index, grid)
index += 1
result = max(area.values() or [0])
for r in xrange(len(grid)):
for c in xrange(len(grid[r])):
if grid[r][c] == 0:
seen = set()
for d in directions:
nr, nc = r+d[0], c+d[1]
if not (0 <= nr < len(grid) and
0 <= nc < len(grid[0]) and
grid[nr][nc] > 1):
continue
seen.add(grid[nr][nc])
result = max(result, 1 + sum(area[i] for i in seen))
return result
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。