From 9ba58c4b4cad3d67b2ac39a69a04fb12608c2c2d Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 1 Feb 2026 05:01:03 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_3.py | 17 ++++++++++ .../common_algos/valid_palindrome_round_3.py | 23 ++++++++++++++ ...kids_with_the_greates_number_of_candies.py | 11 +++++++ .../round_5/longest_common_subsequence.py | 21 +++++++++++++ .../lowest_common_ancestor_binary_tree_iv.py | 31 +++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_5/kids_with_the_greates_number_of_candies.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_5/longest_common_subsequence.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_5/lowest_common_ancestor_binary_tree_iv.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py new file mode 100644 index 00000000..a3a7d986 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py @@ -0,0 +1,17 @@ +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_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py new file mode 100644 index 00000000..b68053bd --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.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=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 \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/kids_with_the_greates_number_of_candies.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/kids_with_the_greates_number_of_candies.py new file mode 100644 index 00000000..5350a8ba --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_5/kids_with_the_greates_number_of_candies.py @@ -0,0 +1,11 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: + max_candies = max(candies) + return [candy + extraCandies >= max_candies for candy in candies] + + + + diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/longest_common_subsequence.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/longest_common_subsequence.py new file mode 100644 index 00000000..f5cc53d5 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_5/longest_common_subsequence.py @@ -0,0 +1,21 @@ +from typing import List, Union, Collection, Mapping, Optional + +class Solution: + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + + # Make a grid of 0's with len(text2) + 1 columns + # and len(text1) + 1 rows. + len_1 = len(text1) + len_2 = len(text2) + dp_grid = [[0]*(len_2+1) for _ in range(len_1+1)] + + # Iterate up each column, starting from the last one. + for j in reversed(range(len_2)): + for i in reversed(range(len_1)): + if text1[i] == text2[j]: + dp_grid[i][j] = dp_grid[i+1][j+1] + 1 + else: + dp_grid[i][j] = max(dp_grid[i+1][j], dp_grid[i][j+1]) + + # The original problem's answer is in dp_grid[0][0]. Return it. + return dp_grid[0][0] diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/lowest_common_ancestor_binary_tree_iv.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/lowest_common_ancestor_binary_tree_iv.py new file mode 100644 index 00000000..d043032f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_5/lowest_common_ancestor_binary_tree_iv.py @@ -0,0 +1,31 @@ +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