diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py new file mode 100644 index 00000000..6c0c6330 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_8.py @@ -0,0 +1,16 @@ +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 [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py new file mode 100644 index 00000000..1ed43720 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_8.py @@ -0,0 +1,23 @@ +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='[^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 \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/number_of_islands.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/number_of_islands.py new file mode 100644 index 00000000..cc03860f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/number_of_islands.py @@ -0,0 +1,67 @@ +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 dfs(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 + dfs(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 + + diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/number_of_ways_to_select_building.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/number_of_ways_to_select_building.py new file mode 100644 index 00000000..dccb5781 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/number_of_ways_to_select_building.py @@ -0,0 +1,44 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def numberOfWays(self, s: str) -> int: + """ + Count valid 3-building selections forming "010" or "101" patterns. + + Key insight: Track how many 2-character patterns we can form, + then extend them with the appropriate third character. + + Time: O(n), Space: O(1) + """ + + count_0 = 0 # Count of '0' seen so far + count_1 = 0 # Count of '1' seen so far + count_01 = 0 # Count of "01" patterns (0 followed by 1) + count_10 = 0 # Count of "10" patterns (1 followed by 0) + + result = 0 + + for char in s: + if char == '0': + # Current '0' can complete "010": we need "01" before this '0' + result += count_01 + + # Current '0' can start new "01" patterns: pair with each previous '0' + # Wait no, "01" means 0 then 1, so we can't create "01" with another 0 + + # Current '0' extends all previous '1' to form "10" pattern + count_10 += count_1 + + count_0 += 1 + else: # char == '1' + # Current '1' can complete "101": we need "10" before this '1' + result += count_10 + + # Current '1' extends all previous '0' to form "01" pattern + count_01 += count_0 + + count_1 += 1 + + return result + diff --git a/src/my_project/interviews/top_150_questions_round_21/invert_binary_tree.py b/src/my_project/interviews/top_150_questions_round_21/invert_binary_tree.py new file mode 100644 index 00000000..70eddd87 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/invert_binary_tree.py @@ -0,0 +1,21 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def invertTree(self, root: TreeNode) -> TreeNode: + + try: + root.val + except: + return root + + root.left, root.right = (self.invertTree(root.right), + self.invertTree(root.left)) + + return root \ No newline at end of file diff --git a/tests/test_150_questions_round_21/test_invert_binary_tree_round_21.py b/tests/test_150_questions_round_21/test_invert_binary_tree_round_21.py new file mode 100644 index 00000000..74f1f98a --- /dev/null +++ b/tests/test_150_questions_round_21/test_invert_binary_tree_round_21.py @@ -0,0 +1,19 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.invert_binary_tree import Solution, TreeNode + +class InvertTreeTestCase(unittest.TestCase): + + def test_none_inverted_tree(self): + solution = Solution() + tree = None + output = solution.invertTree(root=tree) + self.assertIsNone(output) + + def test_inverted_tree(self): + solution = Solution() + tree = TreeNode(1,TreeNode(2),TreeNode(3)) + output = solution.invertTree(root=tree) + self.assertEqual(1,output.val) + self.assertEqual(2,output.right.val) + self.assertEqual(3,output.left.val) \ No newline at end of file