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..c7f907c0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.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_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py new file mode 100644 index 00000000..fa21021b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.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_118_find_k_pairs_with_smallest_sums.py b/src/my_project/interviews/top_150_questions_round_22/ex_118_find_k_pairs_with_smallest_sums.py new file mode 100644 index 00000000..c7f4b74d --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_118_find_k_pairs_with_smallest_sums.py @@ -0,0 +1,19 @@ +import heapq +from typing import List + + +class Solution: + def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: + result = [] + heap = [] + + for i in range(min(k, len(nums1))): + heapq.heappush(heap, (nums1[i] + nums2[0], i, 0)) + + while heap and len(result) < k: + _, i, j = heapq.heappop(heap) + result.append([nums1[i], nums2[j]]) + if j + 1 < len(nums2): + heapq.heappush(heap, (nums1[i] + nums2[j + 1], i, j + 1)) + + return result diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_118_find_k_pairs_with_smallest_sums.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_118_find_k_pairs_with_smallest_sums.ts new file mode 100644 index 00000000..929014f9 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_118_find_k_pairs_with_smallest_sums.ts @@ -0,0 +1,55 @@ +function kSmallestPairs(nums1: number[], nums2: number[], k: number): number[][] { + const result: number[][] = []; + const heap: [number, number, number][] = []; // [sum, i, j] + + const heapPush = (item: [number, number, number]) => { + heap.push(item); + let i = heap.length - 1; + while (i > 0) { + const parent = (i - 1) >> 1; + if (heap[i][0] < heap[parent][0]) { + [heap[i], heap[parent]] = [heap[parent], heap[i]]; + i = parent; + } else break; + } + }; + + const heapPop = (): [number, number, number] | undefined => { + if (heap.length === 0) return undefined; + const top = heap[0]; + const last = heap.pop()!; + if (heap.length > 0) { + heap[0] = last; + let i = 0; + while (true) { + const left = 2 * i + 1; + const right = 2 * i + 2; + let smallest = i; + if (left < heap.length && heap[left][0] < heap[smallest][0]) smallest = left; + if (right < heap.length && heap[right][0] < heap[smallest][0]) smallest = right; + if (smallest === i) break; + [heap[i], heap[smallest]] = [heap[smallest], heap[i]]; + i = smallest; + } + } + return top; + }; + + // Initialize heap with first element of nums1 paired with first element of nums2 + for (let i = 0; i < Math.min(k, nums1.length); i++) { + heapPush([nums1[i] + nums2[0], i, 0]); + } + + while (heap.length > 0 && result.length < k) { + const item = heapPop(); + if (!item) break; + const [_, i, j] = item; + result.push([nums1[i], nums2[j]]); + + if (j + 1 < nums2.length) { + heapPush([nums1[i] + nums2[j + 1], i, j + 1]); + } + } + + return result; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_118_find_k_pairs_with_smallest_sums_round_22.py b/tests/test_150_questions_round_22/test_118_find_k_pairs_with_smallest_sums_round_22.py new file mode 100644 index 00000000..4f8852a1 --- /dev/null +++ b/tests/test_150_questions_round_22/test_118_find_k_pairs_with_smallest_sums_round_22.py @@ -0,0 +1,73 @@ +import unittest +from typing import List +from src.my_project.interviews.top_150_questions_round_22\ + .ex_118_find_k_pairs_with_smallest_sums import Solution + + +class FindKPairsWithSmallestSumsTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + """Test: nums1=[1,7,11], nums2=[2,4,6], k=3, expected=[[1,2],[1,4],[1,6]]""" + nums1 = [1, 7, 11] + nums2 = [2, 4, 6] + k = 3 + expected = [[1, 2], [1, 4], [1, 6]] + result = self.solution.kSmallestPairs(nums1, nums2, k) + self.assertEqual(result, expected) + + def test_example_2(self): + """Test: nums1=[1,1,2], nums2=[1,2,3], k=2, expected=[[1,1],[1,1]]""" + nums1 = [1, 1, 2] + nums2 = [1, 2, 3] + k = 2 + expected = [[1, 1], [1, 1]] + result = self.solution.kSmallestPairs(nums1, nums2, k) + self.assertEqual(result, expected) + + def test_k_equals_total_pairs(self): + """Test: k equals total number of pairs""" + nums1 = [1, 2] + nums2 = [3, 4] + k = 4 + expected = [[1, 3], [1, 4], [2, 3], [2, 4]] + result = self.solution.kSmallestPairs(nums1, nums2, k) + self.assertEqual(result, expected) + + def test_single_element_each(self): + """Test: nums1=[1], nums2=[2], k=1, expected=[[1,2]]""" + nums1 = [1] + nums2 = [2] + k = 1 + expected = [[1, 2]] + result = self.solution.kSmallestPairs(nums1, nums2, k) + self.assertEqual(result, expected) + + def test_k_larger_than_nums1(self): + """Test: k larger than len(nums1), uses all nums1 elements initially""" + nums1 = [1, 2, 3] + nums2 = [1, 2, 3] + k = 4 + expected = [[1, 1], [1, 2], [2, 1], [1, 3]] + result = self.solution.kSmallestPairs(nums1, nums2, k) + self.assertEqual(sorted(result), sorted(expected)) + + def test_negative_numbers(self): + """Test: arrays contain negative numbers""" + nums1 = [-4, -2, 0] + nums2 = [-3, -1, 2] + k = 3 + result = self.solution.kSmallestPairs(nums1, nums2, k) + self.assertEqual(len(result), 3) + sums = [pair[0] + pair[1] for pair in result] + self.assertEqual(sums, sorted(sums)) + + def test_k_equals_1(self): + """Test: k=1 returns only the pair with the smallest sum""" + nums1 = [1, 7, 11] + nums2 = [2, 4, 6] + k = 1 + expected = [[1, 2]] + result = self.solution.kSmallestPairs(nums1, nums2, k) + self.assertEqual(result, expected)