commit c8218849eece650cf9e68e3a045748d971a01b7c
parent 645b8954b236b5417fb0d1e51425b3dfcf11bc2e
Author: Andrew Laack <andrew@laack.co>
Date: Wed, 16 Jul 2025 15:02:24 -0500
Completed max area of island
Diffstat:
1 file changed, 34 insertions(+), 0 deletions(-)
diff --git a/max-area-of-island/max-area-of-islandV1.py b/max-area-of-island/max-area-of-islandV1.py
@@ -0,0 +1,34 @@
+class Solution:
+ def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
+
+ max_size = 0
+
+ for y in range(len(grid)):
+ for x in range(len(grid[y])):
+ if grid[y][x] == 1:
+ current = count_and_mark(x, y, grid)
+
+ if current > max_size:
+ max_size = current
+
+ return max_size
+
+
+def count_and_mark(x_pos, y_pos, grid) -> int:
+ if grid[y_pos][x_pos] != 1:
+ return 0
+
+ count = 1
+ grid[y_pos][x_pos] = -1
+
+ if x_pos > 0:
+ count += count_and_mark(x_pos - 1, y_pos, grid)
+ if y_pos > 0:
+ count += count_and_mark(x_pos, y_pos - 1, grid)
+
+ if y_pos < len(grid) - 1:
+ count += count_and_mark(x_pos, y_pos + 1, grid)
+ if x_pos < len(grid[0]) - 1:
+ count += count_and_mark(x_pos + 1, y_pos, grid)
+
+ return count