From 0b9885d50b7bc6ee831795c2ba84c1169022867f Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 9 Nov 2025 04:20:11 -0600 Subject: [PATCH] adding algo --- .../round_2/valid_palindrome_round_9.py | 23 +++++++++++++++ .../lowest_common_ancestor_binary_tree_iv.py | 29 +++++++++++++++++++ .../minimum_swaps_to_group_all_1_together.py | 21 ++++++++++++++ .../top_150_questions_round_21/two_sum.py | 16 ++++++++++ .../test_two_sum_round_21.py | 18 ++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_9.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_3/lowest_common_ancestor_binary_tree_iv.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_swaps_to_group_all_1_together.py create mode 100644 src/my_project/interviews/top_150_questions_round_21/two_sum.py create mode 100644 tests/test_150_questions_round_21/test_two_sum_round_21.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_9.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_9.py new file mode 100644 index 00000000..54820f63 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_9.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 it 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/lowest_common_ancestor_binary_tree_iv.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/lowest_common_ancestor_binary_tree_iv.py new file mode 100644 index 00000000..71d4191d --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/lowest_common_ancestor_binary_tree_iv.py @@ -0,0 +1,29 @@ +from typing import List, Union, Collection, Mapping, Optional + +# Definition for a binary tree node. +class TreeNode: + def __init__(self, x): + self.val = x + self.left = None + self.right = None + +class Solution: + def lowestCommonAncestor(self, root: TreeNode, nodes: List[TreeNode]) -> 'TreeNode': + + node_set = set(nodes) + + def dfs(node: TreeNode): + + if not node or node in node_set: + return node + + left = dfs(node.left) + right = dfs(node.right) + + if left and right: + return node + + return left if left else right + + return dfs(root) + \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_swaps_to_group_all_1_together.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_swaps_to_group_all_1_together.py new file mode 100644 index 00000000..29a816f8 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_swaps_to_group_all_1_together.py @@ -0,0 +1,21 @@ +from typing import List, Union, Collection, Mapping, Optional + +class Solution: + def minSwaps(self, data: List[int]) -> int: + + # Window size + k = sum(data) + + answer = val = 0 + + for i, v in enumerate(data): + + val += v + + if i >= k: + val -= data[i - k] + + if i >= k - 1: + answer = max(answer, val) + + return k - answer \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_21/two_sum.py b/src/my_project/interviews/top_150_questions_round_21/two_sum.py new file mode 100644 index 00000000..723164d9 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/two_sum.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 -1 \ No newline at end of file diff --git a/tests/test_150_questions_round_21/test_two_sum_round_21.py b/tests/test_150_questions_round_21/test_two_sum_round_21.py new file mode 100644 index 00000000..79844786 --- /dev/null +++ b/tests/test_150_questions_round_21/test_two_sum_round_21.py @@ -0,0 +1,18 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.two_sum import Solution + +class TwoSumTestCase(unittest.TestCase): + + def test_is_two_sum(self): + solution = Solution() + output = solution.twoSum(nums=[2,7,11,15], target=9) + target = [0,1] + for k, v in enumerate(target): + self.assertEqual(v, output[k]) + + def test_is_no_two_sum(self): + solution = Solution() + output = solution.twoSum(nums=[2,7,11,15], target=0) + target = -1 + self.assertEqual(output, target) \ No newline at end of file