From 23138d4d052f76a175c73c66d8c13bb6a27f6bf9 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 11 Aug 2026 04:49:19 -0600 Subject: [PATCH 1/2] adding updates --- .../round_7/number_of_islands.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_7/number_of_islands.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_7/number_of_islands.py b/src/my_project/interviews/amazon_high_frequency_23/round_7/number_of_islands.py new file mode 100644 index 00000000..32a4ae66 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_7/number_of_islands.py @@ -0,0 +1,66 @@ +from typing import List, Union, Collection, Mapping, Optional +from collections import deque + +class Solution: + def numIslands(self, grid: List[List[str]]) -> int: + """BFS approach - explores island level by level""" + + if not grid: + return 0 + + rows, cols = len(grid), len(grid[0]) + islands = 0 + + def bfs(r: int, c: int): + queue = deque([(r,c)]) + grid[r][c] = '0' + + while queue: + row, col = queue.popleft() + + for dr, dc in [(1,0), (-1,0), (0,1), (0,-1)]: + nr, nc = row + dr, col + dc + if (0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == '1'): + grid[nr][nc] = '0' + queue.append((nr,nc)) + + for r in range(rows): + for c in range(cols): + if grid[r][c] == '1': + islands += 1 + bfs(r,c) + + return islands + + + + + def numIslands_DFS(grid: List[List[str]]) -> int: + """DFS approach - explores island depth-first""" + + if not grid: + return 0 + + rows, cols = len(grid), len(grid[0]) + islands = 0 + + def dfs(r: int, c: int): + if (r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == '0'): + return + + grid[r][c] = '0' + + dfs(r-1,c) + dfs(r+1,c) + dfs(r,c-1) + dfs(r,c+1) + + for r in range(rows): + for c in range(cols): + if grid[r][c] == '1': + islands += 1 + dfs(r,c) + + return islands + + From c9e886088c4da58551b127e7e92403ed720c19c0 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 11 Aug 2026 04:49:54 -0600 Subject: [PATCH 2/2] adding updates --- .../round_7/number_of_islands.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_7/number_of_islands.py b/src/my_project/interviews/amazon_high_frequency_23/round_7/number_of_islands.py index 32a4ae66..da76f9ab 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/round_7/number_of_islands.py +++ b/src/my_project/interviews/amazon_high_frequency_23/round_7/number_of_islands.py @@ -5,7 +5,7 @@ class Solution: def numIslands(self, grid: List[List[str]]) -> int: """BFS approach - explores island level by level""" - if not grid: + if not grid: return 0 rows, cols = len(grid), len(grid[0]) @@ -15,14 +15,14 @@ def bfs(r: int, c: int): queue = deque([(r,c)]) grid[r][c] = '0' - while queue: + while queue: row, col = queue.popleft() - for dr, dc in [(1,0), (-1,0), (0,1), (0,-1)]: - nr, nc = row + dr, col + dc + for dr, dc in [(1,0),(-1,0),(0,1),(0,-1)]: + nr, nc = row + dr, col + dc if (0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == '1'): grid[nr][nc] = '0' - queue.append((nr,nc)) + queue.append((nr, nc)) for r in range(rows): for c in range(cols): @@ -33,6 +33,8 @@ def bfs(r: int, c: int): return islands + + def numIslands_DFS(grid: List[List[str]]) -> int: