From 1738e6bccdb6f006d5c35f14b90b88ae2bfc7eaf Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 25 Nov 2025 04:30:12 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_17.py | 16 ++++++++++ .../common_algos/valid_palindrome_round_17.py | 23 +++++++++++++ ...erations_to_turn_array_into_a_palindrom.py | 31 ++++++++++++++++++ .../minimum_swaps_to_group_all_ones.py | 32 +++++++++++++++++++ .../top_150_questions_round_21/sqrtx.py | 20 ++++++++++++ .../test_sqrtx_round_21.py | 17 ++++++++++ 6 files changed, 139 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_17.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_17.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_4/minimum_number_of_operations_to_turn_array_into_a_palindrom.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_4/minimum_swaps_to_group_all_ones.py create mode 100644 src/my_project/interviews/top_150_questions_round_21/sqrtx.py create mode 100644 tests/test_150_questions_round_21/test_sqrtx_round_21.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_17.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_17.py new file mode 100644 index 00000000..2d8643f2 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_17.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_17.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_17.py new file mode 100644 index 00000000..2ecd58b3 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_17.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_4/minimum_number_of_operations_to_turn_array_into_a_palindrom.py b/src/my_project/interviews/amazon_high_frequency_23/round_4/minimum_number_of_operations_to_turn_array_into_a_palindrom.py new file mode 100644 index 00000000..6e58e6a7 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_4/minimum_number_of_operations_to_turn_array_into_a_palindrom.py @@ -0,0 +1,31 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def minimumOperations(self, nums: List[int]) -> int: + + l, r = 0, len(nums) - 1 + ls = nums[l] + rs = nums[r] + answer = 0 + + while l < r: + + if ls == rs: + l += 1 + r -= 1 + ls = nums[l] + rs = nums[r] + elif ls < rs: + l += 1 + ls += nums[l] + answer += 1 + else: + r -= 1 + rs += nums[r] + answer += 1 + + return answer + + + diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_4/minimum_swaps_to_group_all_ones.py b/src/my_project/interviews/amazon_high_frequency_23/round_4/minimum_swaps_to_group_all_ones.py new file mode 100644 index 00000000..f9994702 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_4/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). +''' diff --git a/src/my_project/interviews/top_150_questions_round_21/sqrtx.py b/src/my_project/interviews/top_150_questions_round_21/sqrtx.py new file mode 100644 index 00000000..a51a465b --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/sqrtx.py @@ -0,0 +1,20 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def mySqrt(self, x: int) -> int: + + left, right = 0, x + + while left <= right: + + mid = (left + right)//2 + + if mid ** 2 < x: + left = mid + 1 + elif mid ** 2 > x: + right = mid - 1 + else: + return mid + + return min(left, right) \ No newline at end of file diff --git a/tests/test_150_questions_round_21/test_sqrtx_round_21.py b/tests/test_150_questions_round_21/test_sqrtx_round_21.py new file mode 100644 index 00000000..05d9e4c2 --- /dev/null +++ b/tests/test_150_questions_round_21/test_sqrtx_round_21.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.sqrtx import Solution + +class SqrtxTestCase(unittest.TestCase): + + def test_even_sqrtx(self): + solution = Solution() + output = solution.mySqrt(x=4) + target = 2 + self.assertEqual(output, target) + + def test_odd_sqrtx(self): + solution = Solution() + output = solution.mySqrt(x=8) + target = 2 + self.assertEqual(output, target) \ No newline at end of file