From f92548b245dd99bbd7475dcae567a67a3806e2c3 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 22 Feb 2026 04:34:50 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_11.py | 19 +++++ .../common_algos/valid_palindrome_round_11.py | 22 +++++ .../ex_84_number_of_islands.py | 64 +++++++++++++++ .../ex_84_number_of_islands.ts | 25 ++++++ .../test_84_number_of_islands_round_22.py | 81 +++++++++++++++++++ 5 files changed, 211 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_11.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_11.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_84_number_of_islands.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_84_number_of_islands.ts create mode 100644 tests/test_150_questions_round_22/test_84_number_of_islands_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_11.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_11.py new file mode 100644 index 00000000..a7ecea5f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_11.py @@ -0,0 +1,19 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + + answer = dict() + + for k, v in enumerate(nums): + + if v in answer: + return [answer[v], k] + else: + answer[target - v] = k + + return [] + + + diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_11.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_11.py new file mode 100644 index 00000000..197be03f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_11.py @@ -0,0 +1,22 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + + # To lowercase + s = s.lower() + + # Remove non-alphanumeric characters + s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) + + # Determine if s is palindrome or not + len_s = len(s) + + for i in range(len_s//2): + + if s[i] != s[len_s - 1 - i]: + return False + + return True diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_84_number_of_islands.py b/src/my_project/interviews/top_150_questions_round_22/ex_84_number_of_islands.py new file mode 100644 index 00000000..f66cc56a --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_84_number_of_islands.py @@ -0,0 +1,64 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +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(self, 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 + diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_84_number_of_islands.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_84_number_of_islands.ts new file mode 100644 index 00000000..a4ce79a4 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_84_number_of_islands.ts @@ -0,0 +1,25 @@ +let m: number, n: number +function numIslands(grid: string[][]): number { + let count: number = 0 + if (!grid || !grid[0]) return count + m = grid.length + n = grid[0].length + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (grid[i][j] === "1") { + count++ + dfs(grid, i, j) + } + } + } + return count +}; + +function dfs(grid: string[][], i: number, j: number) { + if (i >= m || j >= n || i < 0 || j < 0 || grid[i][j] === '0') return + grid[i][j] = '0' + dfs(grid, i + 1, j) + dfs(grid, i - 1, j) + dfs(grid, i, j - 1) + dfs(grid, i, j + 1) +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_84_number_of_islands_round_22.py b/tests/test_150_questions_round_22/test_84_number_of_islands_round_22.py new file mode 100644 index 00000000..1fe06a58 --- /dev/null +++ b/tests/test_150_questions_round_22/test_84_number_of_islands_round_22.py @@ -0,0 +1,81 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_84_number_of_islands import Solution + +class NumberOfIslandsTescaseCase(unittest.TestCase): + + def test_example_1(self): + # Example 1: Single island + # Input: grid = [ + # ["1","1","1","1","0"], + # ["1","1","0","1","0"], + # ["1","1","0","0","0"], + # ["0","0","0","0","0"] + # ] + # Output: 1 + solution = Solution() + grid = [ + ["1", "1", "1", "1", "0"], + ["1", "1", "0", "1", "0"], + ["1", "1", "0", "0", "0"], + ["0", "0", "0", "0", "0"] + ] + result = solution.numIslands(grid) + self.assertEqual(result, 1) + + def test_example_2(self): + # Example 2: Three islands + # Input: grid = [ + # ["1","1","0","0","0"], + # ["1","1","0","0","0"], + # ["0","0","1","0","0"], + # ["0","0","0","1","1"] + # ] + # Output: 3 + solution = Solution() + grid = [ + ["1", "1", "0", "0", "0"], + ["1", "1", "0", "0", "0"], + ["0", "0", "1", "0", "0"], + ["0", "0", "0", "1", "1"] + ] + result = solution.numIslands(grid) + self.assertEqual(result, 3) + + def test_example_1_dfs(self): + # Example 1: Single island (DFS approach) + # Input: grid = [ + # ["1","1","1","1","0"], + # ["1","1","0","1","0"], + # ["1","1","0","0","0"], + # ["0","0","0","0","0"] + # ] + # Output: 1 + solution = Solution() + grid = [ + ["1", "1", "1", "1", "0"], + ["1", "1", "0", "1", "0"], + ["1", "1", "0", "0", "0"], + ["0", "0", "0", "0", "0"] + ] + result = solution.numIslands_DFS(grid) + self.assertEqual(result, 1) + + def test_example_2_dfs(self): + # Example 2: Three islands (DFS approach) + # Input: grid = [ + # ["1","1","0","0","0"], + # ["1","1","0","0","0"], + # ["0","0","1","0","0"], + # ["0","0","0","1","1"] + # ] + # Output: 3 + solution = Solution() + grid = [ + ["1", "1", "0", "0", "0"], + ["1", "1", "0", "0", "0"], + ["0", "0", "1", "0", "0"], + ["0", "0", "0", "1", "1"] + ] + result = solution.numIslands_DFS(grid) + self.assertEqual(result, 3) \ No newline at end of file