From f1525634de1f02ed8beba85f63e63cba89e059a2 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 2 Feb 2026 04:49:33 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_4.py | 19 ++++++++++++++ .../common_algos/valid_palindrome_round_4.py | 22 ++++++++++++++++ ...maximum_sum_distinct_subarrays_lenght_k.py | 25 +++++++++++++++++++ .../round_5/merge_intervals.py | 17 +++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_5/maximum_sum_distinct_subarrays_lenght_k.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_5/merge_intervals.py 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 new file mode 100644 index 00000000..b642cdc8 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.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_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py new file mode 100644 index 00000000..0a9f8734 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.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 \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/maximum_sum_distinct_subarrays_lenght_k.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/maximum_sum_distinct_subarrays_lenght_k.py new file mode 100644 index 00000000..930e00b4 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_5/maximum_sum_distinct_subarrays_lenght_k.py @@ -0,0 +1,25 @@ +from typing import List, Union, Collection, Mapping, Optional + +class Solution: + def maximumSubarraySum(self, nums: List[int], k: int) -> int: + max_sum = 0 + current_sum = 0 + freq_sum = dict() + left = 0 + + for right in range(len(nums)): + current_sum += nums[right] + + freq_sum[nums[right]] = freq_sum.get(nums[right], 0) + 1 + + if right - left > k - 1: + freq_sum[nums[left]] -= 1 + if freq_sum[nums[left]] == 0: + del freq_sum[nums[left]] + current_sum -= nums[left] + left += 1 + + if right - left == k - 1 and len(freq_sum) == k: + max_sum = max(max_sum, current_sum) + + return max_sum \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_5/merge_intervals.py b/src/my_project/interviews/amazon_high_frequency_23/round_5/merge_intervals.py new file mode 100644 index 00000000..e90ad800 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_5/merge_intervals.py @@ -0,0 +1,17 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def merge(self, intervals: List[List[int]]) -> List[List[int]]: + + list_answer = list() + + for i in sorted(intervals, key=lambda x: x[0]): + + if list_answer and i[0] <= list_answer[-1][-1]: + list_answer[-1][-1] = max(list_answer[-1][-1], i[-1]) + else: + list_answer.append(i) + + return list_answer +