From 3f6f6b9f25997209c1e5d11e94103cccf7a15810 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 19 Jun 2026 05:06:44 -0600 Subject: [PATCH] adding updates --- .../common_algos/two_sum_round_5.py | 19 +++++++++++ .../common_algos/valid_palindrome_round_5.py | 22 +++++++++++++ .../minimum_swaps_to_group_all_ones.py | 24 ++++++++++---- .../minimum_swaps_to_group_all_ones.py | 32 +++++++++++++++++++ 4 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_6/minimum_swaps_to_group_all_ones.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py new file mode 100644 index 00000000..581881df --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.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_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py new file mode 100644 index 00000000..615d0076 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.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/amazon_high_frequency_23/round_2/minimum_swaps_to_group_all_ones.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/minimum_swaps_to_group_all_ones.py index 2de5deda..90895c3b 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/minimum_swaps_to_group_all_ones.py +++ b/src/my_project/interviews/amazon_high_frequency_23/round_2/minimum_swaps_to_group_all_ones.py @@ -3,13 +3,23 @@ class Solution: def minSwaps(self, data: List[int]) -> int: - k = sum(data) # window size - ans = val = 0 - for i, x in enumerate(data): - val += x - if i >= k: val -= data[i-k] - if i+1 >= k: ans = max(ans, val) - return k - ans + + # Set window size + k = sum(data) + + val = answer = 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 ''' Window size (k): Count total number of 1s in the array. This is the size of the window needed to fit all 1s. diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_6/minimum_swaps_to_group_all_ones.py b/src/my_project/interviews/amazon_high_frequency_23/round_6/minimum_swaps_to_group_all_ones.py new file mode 100644 index 00000000..90895c3b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_6/minimum_swaps_to_group_all_ones.py @@ -0,0 +1,32 @@ +from typing import List, Union, Collection, Mapping, Optional + + +class Solution: + def minSwaps(self, data: List[int]) -> int: + + # Set window size + k = sum(data) + + val = answer = 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 + +''' +Window size (k): Count total number of 1s in the array. This is the size of the window needed to fit all 1s. + +Sliding window: Move a window of size k across the array and count how many 1s are already in each window position. + +Find maximum 1s: Track the maximum number of 1s found in any window (ans). + +Calculate swaps: The minimum swaps needed = k - ans (total 1s minus the maximum 1s already grouped in any window). +'''