From c3f110900465730c931aa267e40d635a93387b29 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 13 Nov 2025 04:33:08 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_5.py | 17 ++++++++++++ .../common_algos/valid_palindrome_round_5.py | 23 ++++++++++++++++ ...maximum_sum_distinct_subarrays_lenght_k.py | 26 +++++++++++++++++++ .../round_3/merge_intervals.py | 16 ++++++++++++ .../valid_parentheses.py | 20 ++++++++++++++ .../test_valid_parentheses_round_21.py | 20 ++++++++++++++ 6 files changed, 122 insertions(+) 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_3/maximum_sum_distinct_subarrays_lenght_k.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_3/merge_intervals.py create mode 100644 src/my_project/interviews/top_150_questions_round_21/valid_parentheses.py create mode 100644 tests/test_150_questions_round_21/test_valid_parentheses_round_21.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..a3a7d986 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.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_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py new file mode 100644 index 00000000..1ed43720 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.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='[^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_3/maximum_sum_distinct_subarrays_lenght_k.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/maximum_sum_distinct_subarrays_lenght_k.py new file mode 100644 index 00000000..34803d58 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/maximum_sum_distinct_subarrays_lenght_k.py @@ -0,0 +1,26 @@ +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 + diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/merge_intervals.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/merge_intervals.py new file mode 100644 index 00000000..c71a5069 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/merge_intervals.py @@ -0,0 +1,16 @@ +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 \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_21/valid_parentheses.py b/src/my_project/interviews/top_150_questions_round_21/valid_parentheses.py new file mode 100644 index 00000000..c230b41f --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/valid_parentheses.py @@ -0,0 +1,20 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def isValid(self, s): + + dic_parentheses = {'(':')','[':']','{':'}'} + + check_list = list() + + for p in s: + + if p in dic_parentheses: + check_list.append(p) + else: + if len(check_list) == 0 \ + or dic_parentheses[check_list.pop()] != p: + return False + + return len(check_list) == 0 \ No newline at end of file diff --git a/tests/test_150_questions_round_21/test_valid_parentheses_round_21.py b/tests/test_150_questions_round_21/test_valid_parentheses_round_21.py new file mode 100644 index 00000000..69f2a106 --- /dev/null +++ b/tests/test_150_questions_round_21/test_valid_parentheses_round_21.py @@ -0,0 +1,20 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.valid_parentheses import Solution + +class ValidParenthesesTestCase(unittest.TestCase): + + def test_is_valid_parentheses(self): + solution = Solution() + output = solution.isValid(s='()') + self.assertTrue(output) + + def test_is_no_valid_parentheses_pattern_1(self): + solution = Solution() + output = solution.isValid(s='(]') + self.assertFalse(output) + + def test_is_no_valid_parentheses_pattern_2(self): + solution = Solution() + output = solution.isValid(s='((') + self.assertFalse(output) \ No newline at end of file