-
Notifications
You must be signed in to change notification settings - Fork 0
/
lc_0827_large_island.py
56 lines (43 loc) · 1.43 KB
/
lc_0827_large_island.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
import unittest
from typing import List
class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
N = len(grid)
R = range(N)
def compact(x, y):
return -x * N - y - 1
def neighbors(x, y):
return set(
n
for n in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
if all(c in R for c in n)
)
g = {(x, y): grid[x][y] for x in R for y in R}
def dfs(p, i):
if g.get(p) == 1:
yield p
g[p] = i
for n in neighbors(*p):
yield from dfs(n, i)
isles = {compact(*k): len(list(dfs(k, compact(*k)))) for k in g}
return max(
list(isles.values())
+ [
sum(isles[isle] for isle in set(g[n] for n in neighbors(*k)) if isle)
+ 1
for k, v in g.items()
if not v
]
)
class MyTestCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_empty(self):
grid = [[0, 0], [0, 0]]
self.assertEqual(self.solution.largestIsland(grid), 1)
def test_three(self):
grid = [[0, 1], [1, 0]]
self.assertEqual(self.solution.largestIsland(grid), 3)
def test_square(self):
grid = [[1, 1], [1, 1]]
self.assertEqual(self.solution.largestIsland(grid), 4)