Skip to content

Commit 5c3ad98

Browse files
committed
Daily hard
1 parent a3c490e commit 5c3ad98

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

my-submissions/h827.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This can basically boil down to an island question where you
2+
track the sizes of each island and find the cell with the largest sum
3+
of its NSEW tile sum (North, South, etc.).
4+
5+
Edge case: avoid if an island borders a cell more than once

my-submissions/h827.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# grid value: 0 | 1 ===>>> new grid value: -(island_id)
2+
class Solution:
3+
def largestIsland(self, grid: List[List[int]]) -> int:
4+
# Count grid tiles and set the new grid tile to be a negative
5+
# value: the island's assigned ID but negative
6+
# This marks islands as visited
7+
def propogate_grid(r: int, c: int, grid: List[List[int]], island_id: int) -> int :
8+
grid[r][c] = -island_id
9+
size = 1
10+
if r > 0 and grid[r - 1][c] > 0 :
11+
size += propogate_grid(r - 1, c, grid, island_id)
12+
if r < len(grid) - 1 and grid[r + 1][c] > 0 :
13+
size += propogate_grid(r + 1, c, grid, island_id)
14+
if c > 0 and grid[r][c - 1] > 0 :
15+
size += propogate_grid(r, c - 1, grid, island_id)
16+
if c < len(grid[0]) - 1 and grid[r][c + 1] > 0 :
17+
size += propogate_grid(r, c + 1, grid, island_id)
18+
return size
19+
20+
maxx_island = 0
21+
island_id_counter = 1
22+
island_sizes = {} # {island_id: size}
23+
for r in range(len(grid)) :
24+
for c in range(len(grid[0])) :
25+
if grid[r][c] > 0 :
26+
island_size = propogate_grid(r, c, grid, island_id_counter)
27+
island_sizes[island_id_counter] = island_size
28+
maxx_island = max(maxx_island, island_size)
29+
island_id_counter += 1
30+
31+
# If the entire grid is occupied, that's the max size
32+
# If not, then we can at least add one tile to the largest
33+
# island e.g. if there's only one island
34+
if maxx_island == len(grid) * len(grid[0]) :
35+
return maxx_island
36+
else :
37+
maxx_island += 1
38+
39+
for r in range(len(grid)) :
40+
for c in range(len(grid[0])) :
41+
if grid[r][c] == 0 :
42+
large_island = 1
43+
ids = set() # We don't want to add an island twice
44+
if r > 0 and grid[r - 1][c] :
45+
ids.add(grid[r - 1][c])
46+
large_island += island_sizes[-grid[r - 1][c]]
47+
if r < len(grid) - 1 and grid[r + 1][c] and grid[r + 1][c] not in ids :
48+
ids.add(grid[r + 1][c])
49+
large_island += island_sizes[-grid[r + 1][c]]
50+
if c > 0 and grid[r][c - 1] and grid[r][c - 1] not in ids :
51+
ids.add(grid[r][c - 1])
52+
large_island += island_sizes[-grid[r][c - 1]]
53+
if c < len(grid[0]) - 1 and grid[r][c + 1] and grid[r][c + 1] not in ids :
54+
large_island += island_sizes[-grid[r][c + 1]]
55+
maxx_island = max(maxx_island, large_island)
56+
57+
return maxx_island

0 commit comments

Comments
 (0)