From 73eec5d942d87f8806c45c788909d7f2c084cefd Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 30 Jan 2026 05:17:19 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_1.py | 4 +- .../common_algos/two_sum_round_2.py | 16 --- .../common_algos/two_sum_round_3.py | 16 --- .../common_algos/two_sum_round_4.py | 16 --- .../common_algos/valid_palindrome_round_1.py | 5 +- .../common_algos/valid_palindrome_round_2.py | 22 ---- .../common_algos/valid_palindrome_round_3.py | 22 ---- .../common_algos/valid_palindrome_round_4.py | 22 ---- .../round_5/design_sql.py | 3 +- .../find_minimum_time_to_finsh_all_jobs_ii.py | 8 +- .../round_5/jump_game_ii.py | 33 ------ .../round_5/jump_game_vii.py | 61 ---------- .../round_5/k_free_subsets.py | 110 ------------------ ...kids_with_the_greates_number_of_candies.py | 7 -- .../round_5/longest_common_subsequence.py | 22 ---- .../lowest_common_ancestor_binary_tree_iv.py | 29 ----- 16 files changed, 9 insertions(+), 387 deletions(-) delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_5/jump_game_ii.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_5/jump_game_vii.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_5/k_free_subsets.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_5/kids_with_the_greates_number_of_candies.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_5/longest_common_subsequence.py delete 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_1.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_1.py index 534c7ec3..e03fe46f 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_1.py +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_1.py @@ -10,7 +10,7 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: if v in answer: return [answer[v], k] - else: + 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/two_sum_round_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py deleted file mode 100644 index 6c0c6330..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py +++ /dev/null @@ -1,16 +0,0 @@ -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/two_sum_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py deleted file mode 100644 index 6c0c6330..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py +++ /dev/null @@ -1,16 +0,0 @@ -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/two_sum_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py deleted file mode 100644 index 6c0c6330..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py +++ /dev/null @@ -1,16 +0,0 @@ -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_1.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_1.py index cad2b1e6..40d81dab 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_1.py +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_1.py @@ -11,11 +11,12 @@ def isPalindrome(self, s: str) -> bool: # Remove non-alphanumeric characters s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) - # Determine if s is palindrome or not + # 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 diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py deleted file mode 100644 index 8bfb7f9e..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py +++ /dev/null @@ -1,22 +0,0 @@ -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/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 deleted file mode 100644 index 68520882..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py +++ /dev/null @@ -1,22 +0,0 @@ -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/common_algos/valid_palindrome_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py deleted file mode 100644 index 6b7cc68b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py +++ /dev/null @@ -1,22 +0,0 @@ -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) - - # Determinve 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/design_sql.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/design_sql.py index 3e929650..d6a41c09 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/round_5/design_sql.py +++ b/src/my_project/interviews/amazon_high_frequency_23/round_5/design_sql.py @@ -1,5 +1,6 @@ from typing import List, Union, Collection, Mapping, Optional, Dict + class Table: def __init__(self, name:str, columns: int, rows: Dict, row_count: int = 0): @@ -98,8 +99,6 @@ def exp(self, name: str) -> List[str]: result.append(f"{row_id},{row_str}") return result - - diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/find_minimum_time_to_finsh_all_jobs_ii.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/find_minimum_time_to_finsh_all_jobs_ii.py index cebb1d31..8bf9a549 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/round_5/find_minimum_time_to_finsh_all_jobs_ii.py +++ b/src/my_project/interviews/amazon_high_frequency_23/round_5/find_minimum_time_to_finsh_all_jobs_ii.py @@ -1,4 +1,4 @@ -from typing import List, Union, Collection, Mapping, Optional +from typing import List, Union, Collection, Mapping, Optional, Dict from abc import ABC, abstractmethod import math @@ -8,7 +8,5 @@ def minimumTime(self, jobs: List[int], workers: List[int]) -> int: jobs.sort() workers.sort() - return max([math.ceil(n/d) for n,d in zip(jobs, workers)]) - - - + return max([math.ceil(n/d) for n, d in zip(jobs, workers)]) + diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/jump_game_ii.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/jump_game_ii.py deleted file mode 100644 index 1ef43897..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_5/jump_game_ii.py +++ /dev/null @@ -1,33 +0,0 @@ -from typing import List - -class Solution: - def jump(self, nums: List[int]) -> int: - """ - Greedy approach: At each position, jump to the farthest reachable index - - Example: [2,3,1,1,4] - - From index 0 (value=2): can reach indices 1,2 - - Greedy choice: Jump to index 1 (value=3) because it reaches farthest - - From index 1: can reach indices 2,3,4 (end) - - Answer: 2 jumps - """ - - if len(nums) <= 1: - return 0 - - jumps = 0 - current_end = 0 # End of current jump range - farthest = 0 # The farthest position we can reach - - for i in range(len(nums) - 1): - # Update farthest position reachable - farthest = max(farthest, i + nums[i]) - - # If we've reached the end of current jump range - if i == current_end: - jumps += 1 - current_end = farthest # Make the greedy choice - - if current_end > len(nums) - 1: - break - return jumps \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/jump_game_vii.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/jump_game_vii.py deleted file mode 100644 index 483e38c5..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_5/jump_game_vii.py +++ /dev/null @@ -1,61 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional - - -class Solution: - def minCost(self, nums: List[int], costs: List[int]) -> int: - """ - Example: nums = [3, 1, 2, 4], costs = [1, 2, 3, 4] - - From index 0 (value=3): - - Next smaller: index 1 (value=1) - - Next larger: index 3 (value=4) - - DP builds cost backwards: - - dp[3] = 0 (at end, no cost) - - dp[2] = dp[3] + costs[3] = 0 + 4 = 4 (can jump to 4) - - dp[1] = dp[2] + costs[2] = 4 + 3 = 7 (can jump to 2) - - dp[0] = min(dp[1]+costs[1], dp[3]+costs[3]) = min(7+2, 0+4) = 4 - """ - - smallStack = [] # Monotonic increasing (next smaller element) - largeStack = [] # Monotonic decreasing (next larger element) - dp = [0] * len(nums) # dp[i] = min cost from i to end - - # Process backwards (right to left) - for i in range(len(nums) - 1, -1, -1): - - # Maintain monotonic increasing stack (for next smaller) - # Remove elements >= current (they can't be "next smaller") - while smallStack and nums[smallStack[-1]] >= nums[i]: - smallStack.pop() - - # Maintain monotonic decreasing stack (for next larger) - # Remove elements < current (they can't be "next larger") - while largeStack and nums[largeStack[-1]] < nums[i]: - largeStack.pop() - - # Calculate minimum cost for this position - nxtCost = [] - - if largeStack: - lid = largeStack[-1] # Next larger index - nxtCost.append(dp[lid] + costs[lid]) - - if smallStack: - sid = smallStack[-1] # Next smaller index - nxtCost.append(dp[sid] + costs[sid]) - - # Min cost from current position to end - dp[i] = min(nxtCost) if nxtCost else 0 - - # Add current index to both stacks - largeStack.append(i) - smallStack.append(i) - - print(smallStack) - print(largeStack) - - - print(dp) - - return dp[0] # Minimum cost from start \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/k_free_subsets.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/k_free_subsets.py deleted file mode 100644 index fac52973..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_5/k_free_subsets.py +++ /dev/null @@ -1,110 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from collections import defaultdict - -class Solution: - def countTheNumOfKFreeSubsets(self, nums: List[int], k: int) -> int: - """ - Count k-Free subsets using dynamic programming. - - Approach: - 1. Group elements by (num % k) to find independent groups - 2. Within each group, sort and build chains where elements differ by k - 3. For each chain, use House Robber DP to count valid subsets - 4. Multiply results across all independent chains - - Time: O(n log n), Space: O(n) - """ - # Group numbers by their remainder when divided by k - groups = defaultdict(list) - for num in nums: - groups[num % k].append(num) - - res = 1 - - # Process each group independently - for group in groups.values(): - group.sort() - - # Build chains within this group - i = 0 - while i < len(group): - chain = [group[i]] - j = i + 1 - - # Build chain where each element is exactly k more than previous - while j < len(group) and group[j] == chain[-1] + k: - chain.append(group[j]) - j += 1 - - # House Robber DP for this chain - m = len(chain) - if m == 1: - chain_res = 2 # {} or {chain[0]} - else: - take = 1 # Take first element - skip = 1 # Skip first element - - for idx in range(1, m): - new_take = skip # Can only take current if we skipped previous - new_skip = take + skip # Can skip current regardless - take, skip = new_take, new_skip - - chain_res = take + skip - - res *= chain_res - i = j - - return res - - - - -''' -Detailed Algorithm Explanation -Part 1: Why Group by num % k? -Two numbers can have a difference of exactly k only if they have the same remainder when divided by k. - -Mathematical proof: - -If a - b = k, then a = b + k -Therefore: a % k = (b + k) % k = b % k -Example: nums = [2, 3, 5, 8], k = 5 - -num | num % 5 | group -----|---------|------- -2 | 2 | Group A -3 | 3 | Group B -5 | 0 | Group C -8 | 3 | Group B - - -Why this matters: Elements from different groups can never differ by k, so they're independent. We can combine any subset from Group A with any subset from Group B. - -Part 2: Building Chains -Within each group, we sort and find chains where consecutive elements differ by exactly k. - -Example with Group B: [3, 8] - -Sorted: [3, 8] -Check: 8 - 3 = 5 ✓ -Chain: 3 → 8 - -Another example: nums = [1, 6, 11, 21], k = 5 (all have remainder 1) - -Sorted: [1, 6, 11, 21] -Check: 6-1=5 ✓, 11-6=5 ✓, 21-11=10 ✗ -Chains: [1 → 6 → 11], [21] - - -Part 3: House Robber DP - The Core Logic -For a chain like [3 → 8], we can't pick both 3 and 8 (they differ by k). This is the House Robber problem: count all subsets where we don't pick adjacent elements. - -DP State Variables -take = number of valid subsets that INCLUDE the current element -skip = number of valid subsets that EXCLUDE the current element - -DP Transitions -new_take = skip # To take current, we MUST have skipped previous -new_skip = take + skip # To skip current, we can take or skip previous - -''' \ 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 deleted file mode 100644 index 7df648c7..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_5/kids_with_the_greates_number_of_candies.py +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index d5d4d0af..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_5/longest_common_subsequence.py +++ /dev/null @@ -1,22 +0,0 @@ -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 deleted file mode 100644 index 71d4191d..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_5/lowest_common_ancestor_binary_tree_iv.py +++ /dev/null @@ -1,29 +0,0 @@ -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