Skip to content

Commit d9196e4

Browse files
committed
Max area of island
1 parent 4a98a58 commit d9196e4

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed

695.max_area_of_island/max.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 695. Max area of island
2+
# Topics: 'Array', 'Depth-First Search', 'Breadth-First Search', 'Union Find', 'Matrix'
3+
# Level: 'Medium'
4+
5+
# You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
6+
7+
# The area of an island is the number of cells with a value 1 in the island.
8+
9+
# Return the maximum area of an island in grid. If there is no island, return 0.
10+
11+
12+
13+
# Example 1:
14+
15+
# Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
16+
# Output: 6
17+
# Explanation: The answer is not 11, because the island must be connected 4-directionally.
18+
19+
# Example 2:
20+
21+
# Input: grid = [[0,0,0,0,0,0,0,0]]
22+
# Output: 0
23+
24+
25+
26+
# Constraints:
27+
28+
# m == grid.length
29+
# n == grid[i].length
30+
# 1 <= m, n <= 50
31+
# grid[i][j] is either 0 or 1.
32+
33+
from typing import List
34+
35+
class Solution:
36+
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
37+
ROWS, COLS = len(grid), len(grid[0])
38+
visits = set()
39+
def dfs(r: int, c: int) -> int:
40+
if min(r,c) < 0:
41+
return 0
42+
if r == ROWS or c == COLS:
43+
return 0
44+
if (r,c) in visits:
45+
return 0
46+
if grid[r][c] == 0:
47+
return 0
48+
visits.add((r,c))
49+
return 1+ dfs(r+1, c)+dfs(r-1,c)+dfs(r, c+1)+dfs(r, c-1)
50+
51+
max_area = 0
52+
for row in range (ROWS):
53+
for col in range (COLS):
54+
if grid[row][col] == 1 and (row,col) not in visits:
55+
max_area = max(max_area, dfs(row, col))
56+
return max_area

695.max_area_of_island/test_max.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import unittest
2+
from typing import List
3+
from max import Solution
4+
5+
class TestMaxAreaOfIsland(unittest.TestCase):
6+
7+
def setUp(self):
8+
self.solution = Solution()
9+
10+
def test_example_1(self):
11+
"""Test with example 1 from problem statement"""
12+
grid = [
13+
[0,0,1,0,0,0,0,1,0,0,0,0,0],
14+
[0,0,0,0,0,0,0,1,1,1,0,0,0],
15+
[0,1,1,0,1,0,0,0,0,0,0,0,0],
16+
[0,1,0,0,1,1,0,0,1,0,1,0,0],
17+
[0,1,0,0,1,1,0,0,1,1,1,0,0],
18+
[0,0,0,0,0,0,0,0,0,0,1,0,0],
19+
[0,0,0,0,0,0,0,1,1,1,0,0,0],
20+
[0,0,0,0,0,0,0,1,1,0,0,0,0]
21+
]
22+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 6)
23+
24+
def test_example_2(self):
25+
"""Test with all zeros (no islands)"""
26+
grid = [[0,0,0,0,0,0,0,0]]
27+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 0)
28+
29+
def test_single_cell_island(self):
30+
"""Test with single cell island"""
31+
grid = [[1]]
32+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 1)
33+
34+
def test_single_cell_water(self):
35+
"""Test with single cell water"""
36+
grid = [[0]]
37+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 0)
38+
39+
def test_entire_grid_is_island(self):
40+
"""Test when entire grid is one island"""
41+
grid = [
42+
[1, 1, 1],
43+
[1, 1, 1],
44+
[1, 1, 1]
45+
]
46+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 9)
47+
48+
def test_multiple_islands_different_sizes(self):
49+
"""Test with multiple islands of different sizes"""
50+
grid = [
51+
[1, 1, 0, 0, 0],
52+
[1, 1, 0, 0, 0],
53+
[0, 0, 0, 1, 1],
54+
[0, 0, 0, 1, 1]
55+
]
56+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 4)
57+
58+
def test_diagonal_not_connected(self):
59+
"""Test that diagonal cells are not considered connected"""
60+
grid = [
61+
[1, 0, 1],
62+
[0, 1, 0],
63+
[1, 0, 1]
64+
]
65+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 1)
66+
67+
def test_l_shaped_island(self):
68+
"""Test L-shaped island"""
69+
grid = [
70+
[1, 0, 0],
71+
[1, 0, 0],
72+
[1, 1, 1]
73+
]
74+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 5)
75+
76+
def test_snake_shaped_island(self):
77+
"""Test snake-shaped island"""
78+
grid = [
79+
[1, 1, 1, 0],
80+
[0, 0, 1, 0],
81+
[0, 0, 1, 0],
82+
[0, 1, 1, 0]
83+
]
84+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 7)
85+
86+
def test_isolated_single_cells(self):
87+
"""Test multiple isolated single-cell islands"""
88+
grid = [
89+
[1, 0, 1, 0, 1],
90+
[0, 0, 0, 0, 0],
91+
[1, 0, 1, 0, 1]
92+
]
93+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 1)
94+
95+
def test_vertical_strip(self):
96+
"""Test vertical strip island"""
97+
grid = [
98+
[0, 1, 0],
99+
[0, 1, 0],
100+
[0, 1, 0],
101+
[0, 1, 0]
102+
]
103+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 4)
104+
105+
def test_horizontal_strip(self):
106+
"""Test horizontal strip island"""
107+
grid = [
108+
[0, 0, 0, 0],
109+
[1, 1, 1, 1],
110+
[0, 0, 0, 0]
111+
]
112+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 4)
113+
114+
def test_complex_shape(self):
115+
"""Test complex island shape"""
116+
grid = [
117+
[1, 1, 0, 1, 1],
118+
[1, 0, 0, 0, 1],
119+
[1, 0, 1, 0, 1],
120+
[1, 1, 1, 1, 1]
121+
]
122+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 14)
123+
124+
def test_max_constraint_size(self):
125+
"""Test with larger grid approaching constraint limits"""
126+
grid = [[1] * 20 for _ in range(20)]
127+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 400)
128+
129+
def test_checkerboard_pattern(self):
130+
"""Test checkerboard pattern (no connected islands)"""
131+
grid = [
132+
[1, 0, 1, 0],
133+
[0, 1, 0, 1],
134+
[1, 0, 1, 0],
135+
[0, 1, 0, 1]
136+
]
137+
self.assertEqual(self.solution.maxAreaOfIsland(grid), 1)
138+
139+
140+
if __name__ == '__main__':
141+
unittest.main()

0 commit comments

Comments
 (0)