diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py new file mode 100644 index 00000000..e4d26440 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.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 [] diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py new file mode 100644 index 00000000..3a6d5cb9 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.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/minimum_number_of_operations_to_turn_array_into_a_palindrom.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_number_of_operations_to_turn_array_into_a_palindrom.py new file mode 100644 index 00000000..20882f81 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_number_of_operations_to_turn_array_into_a_palindrom.py @@ -0,0 +1,28 @@ +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_3/minimum_swaps_to_group_all_1_together.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_swaps_to_group_all_1_together.py deleted file mode 100644 index 29a816f8..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_swaps_to_group_all_1_together.py +++ /dev/null @@ -1,21 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional - -class Solution: - def minSwaps(self, data: List[int]) -> int: - - # Window size - k = sum(data) - - answer = val = 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 \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/minimum_swaps_to_group_all_ones.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/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_3/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/same_tree.py b/src/my_project/interviews/top_150_questions_round_21/same_tree.py new file mode 100644 index 00000000..943ae11d --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/same_tree.py @@ -0,0 +1,18 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: + + if p and q: + return p.val == q.val \ + and self.isSameTree(p.left, q.left) \ + and self.isSameTree(p.right, q.right) + else: + return p is q diff --git a/tests/test_150_questions_round_21/test_same_tree_round_21.py b/tests/test_150_questions_round_21/test_same_tree_round_21.py new file mode 100644 index 00000000..9a1a505d --- /dev/null +++ b/tests/test_150_questions_round_21/test_same_tree_round_21.py @@ -0,0 +1,20 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.same_tree import TreeNode, Solution + +class SameTreeTestCase(unittest.TestCase): + + def test_is_same_tree(self): + solution = Solution() + tree1 = TreeNode(1, TreeNode(2), TreeNode(3)) + tree2 = TreeNode(1, TreeNode(2), TreeNode(3)) + output = solution.isSameTree(p=tree1, q=tree2) + return self.assertTrue(output) + + def test_is_no_same_tree(self): + solution = Solution() + tree1 = TreeNode(1, TreeNode(2), TreeNode(3)) + tree2 = TreeNode(1, TreeNode(3), TreeNode(2)) + output = solution.isSameTree(p=tree1, q=tree2) + return self.assertFalse(output) + \ No newline at end of file