forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0827.py
31 lines (27 loc) · 1.05 KB
/
0827.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
class Solution:
def largestIsland(self, grid):
r, c = len(grid), len(grid[0])
def check(x, y):
if x < 0 or x >= r or y < 0 or y >= c:
return 0
return 1
def dfs(x, y, color):
if not check(x, y) or grid[x][y] != 1:
return 0
res = 1
grid[x][y] = color
for i, j in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
res += dfs(x + i, y + j, color)
return res
data, res, color = [0, 0], 0, 2
for i in range(r):
for j in range(c):
if grid[i][j] == 1:
data.append(dfs(i, j, color))
color += 1
for i in range(r):
for j in range(c):
if grid[i][j] == 0:
colors = set(grid[x+i][y+j] for x, y in [[1, 0], [-1, 0], [0, 1], [0, -1]] if check(i + x, j + y))
res = max(res, sum(data[color] for color in colors) + 1)
return r*c if res == 0 else res