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..c7f907c0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py @@ -0,0 +1,21 @@ +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..fa21021b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.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/top_150_questions_round_22/ex_114_find_minimum_in_rotated_sorted_array.py b/src/my_project/interviews/top_150_questions_round_22/ex_114_find_minimum_in_rotated_sorted_array.py new file mode 100644 index 00000000..06602288 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_114_find_minimum_in_rotated_sorted_array.py @@ -0,0 +1,36 @@ +from typing import List + + +class Solution: + def findMin(self, nums: List[int]) -> int: + """ + Find the minimum element in a rotated sorted array. + + Time Complexity: O(log n) - binary search + Space Complexity: O(1) - constant space + + Args: + nums: A rotated sorted array of unique elements + + Returns: + The minimum element in the array + """ + left = 0 + right = len(nums) - 1 + + # If array is not rotated or has only one element + if nums[left] <= nums[right]: + return nums[left] + + while left < right: + mid = left + (right - left) // 2 + + # If mid element is greater than rightmost element, + # the minimum must be in the right half + if nums[mid] > nums[right]: + left = mid + 1 + else: + # The minimum is in the left half (including mid) + right = mid + + return nums[left] \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_114_find_minimum_in_rotated_sorted_array.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_114_find_minimum_in_rotated_sorted_array.ts new file mode 100644 index 00000000..1555bd84 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_114_find_minimum_in_rotated_sorted_array.ts @@ -0,0 +1,24 @@ +function findMin(nums: number[]): number { + let left = 0; + let right = nums.length - 1; + + // If array is not rotated or has only one element + if (nums[left] <= nums[right]) { + return nums[left]; + } + + while (left < right) { + const mid = Math.floor(left + (right - left) / 2); + + // If mid element is greater than rightmost element, + // the minimum must be in the right half + if (nums[mid] > nums[right]) { + left = mid + 1; + } else { + // The minimum is in the left half (including mid) + right = mid; + } + } + + return nums[left]; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_114_find_minimum_in_rotated_sorted_array_round_22.py b/tests/test_150_questions_round_22/test_114_find_minimum_in_rotated_sorted_array_round_22.py new file mode 100644 index 00000000..1e95fc5e --- /dev/null +++ b/tests/test_150_questions_round_22/test_114_find_minimum_in_rotated_sorted_array_round_22.py @@ -0,0 +1,79 @@ +import unittest +from typing import List +from src.my_project.interviews.top_150_questions_round_22\ + .ex_114_find_minimum_in_rotated_sorted_array import Solution + + +class FindMinimumInRotatedSortedArrayTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1_rotated_3_times(self): + """Test: nums = [3,4,5,1,2], expected = 1""" + nums = [3, 4, 5, 1, 2] + expected = 1 + result = self.solution.findMin(nums) + self.assertEqual(result, expected) + + def test_example_2_rotated_4_times(self): + """Test: nums = [4,5,6,7,0,1,2], expected = 0""" + nums = [4, 5, 6, 7, 0, 1, 2] + expected = 0 + result = self.solution.findMin(nums) + self.assertEqual(result, expected) + + def test_example_3_no_rotation_or_full_rotation(self): + """Test: nums = [11,13,15,17], expected = 11""" + nums = [11, 13, 15, 17] + expected = 11 + result = self.solution.findMin(nums) + self.assertEqual(result, expected) + + def test_single_element(self): + """Test: nums = [1], expected = 1""" + nums = [1] + expected = 1 + result = self.solution.findMin(nums) + self.assertEqual(result, expected) + + def test_two_elements_rotated(self): + """Test: nums = [2,1], expected = 1""" + nums = [2, 1] + expected = 1 + result = self.solution.findMin(nums) + self.assertEqual(result, expected) + + def test_two_elements_not_rotated(self): + """Test: nums = [1,2], expected = 1""" + nums = [1, 2] + expected = 1 + result = self.solution.findMin(nums) + self.assertEqual(result, expected) + + def test_minimum_at_beginning(self): + """Test: nums = [1,2,3,4,5], expected = 1""" + nums = [1, 2, 3, 4, 5] + expected = 1 + result = self.solution.findMin(nums) + self.assertEqual(result, expected) + + def test_minimum_at_end(self): + """Test: nums = [2,3,4,5,1], expected = 1""" + nums = [2, 3, 4, 5, 1] + expected = 1 + result = self.solution.findMin(nums) + self.assertEqual(result, expected) + + def test_minimum_in_middle(self): + """Test: nums = [5,6,7,1,2,3,4], expected = 1""" + nums = [5, 6, 7, 1, 2, 3, 4] + expected = 1 + result = self.solution.findMin(nums) + self.assertEqual(result, expected) + + def test_large_rotation(self): + """Test: nums = [6,7,8,9,10,1,2,3,4,5], expected = 1""" + nums = [6, 7, 8, 9, 10, 1, 2, 3, 4, 5] + expected = 1 + result = self.solution.findMin(nums) + self.assertEqual(result, expected)