From 59e586149a6350d2bae32c9d470cd01ab1e7c018 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 13 Dec 2025 04:26:03 -0600 Subject: [PATCH] adding algo --- .../top_150_questions_round_22/ex_29_3sum.py | 47 +++++++++++++++++++ .../ex_30_minimum_size_subarray_sum.py | 32 +++++++++++++ .../test_29_3sum_round_22.py | 25 ++++++++++ ...t_30_minimum_size_subarray_sum_round_22.py | 23 +++++++++ 4 files changed, 127 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_29_3sum.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_30_minimum_size_subarray_sum.py create mode 100644 tests/test_150_questions_round_22/test_29_3sum_round_22.py create mode 100644 tests/test_150_questions_round_22/test_30_minimum_size_subarray_sum_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_29_3sum.py b/src/my_project/interviews/top_150_questions_round_22/ex_29_3sum.py new file mode 100644 index 00000000..df99d934 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_29_3sum.py @@ -0,0 +1,47 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + + answer = list() + positives = list() + negatives = list() + num_zeroes = 0 + + for num in nums: + if num == 0: + num_zeroes += 1 + elif num > 0: + positives.append(num) + else: + negatives.append(num) + + s_positives = set(positives) + s_negatives = set(negatives) + + for i in range(len(positives)): + for j in range(i+1, len(positives)): + k = positives[i] + positives[j] + if -k in s_negatives: + mi = min(positives[i], positives[j]) + ma = max(positives[i], positives[j]) + answer.append((-k,mi,ma)) + + for i in range(len(negatives)): + for j in range(i+1, len(negatives)): + k = negatives[i] + negatives[j] + if -k in s_positives: + mi = min(negatives[i], negatives[j]) + ma = max(negatives[i], negatives[j]) + answer.append((mi,ma,-k)) + + if num_zeroes >= 1: + for num in s_positives: + if -num in s_negatives: + answer.append((-num,0,num)) + + if num_zeroes >= 3: + answer.append((0,0,0)) + + return list(set(answer)) diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_30_minimum_size_subarray_sum.py b/src/my_project/interviews/top_150_questions_round_22/ex_30_minimum_size_subarray_sum.py new file mode 100644 index 00000000..f522e9a9 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_30_minimum_size_subarray_sum.py @@ -0,0 +1,32 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import math + +class Solution: + def minSubArrayLen(self, target: int, nums: List[int]) -> int: + + window_start = 0 + min_size = float('inf') + + # summation of subarray + sum_subarray = 0 + + # use sliding windows to update min_size of valid subarray + + for window_end, num in enumerate(nums): + + sum_subarray += num + + while sum_subarray >= target: + + # keep shrinking window size if sum_subarray is valid + min_size = min(min_size, window_end-window_start+1) + + # update sum_subarray + sum_subarray -= nums[window_start] + window_start += 1 + + if min_size == float('inf'): + return 0 + else: + return min_size \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_29_3sum_round_22.py b/tests/test_150_questions_round_22/test_29_3sum_round_22.py new file mode 100644 index 00000000..bee834e3 --- /dev/null +++ b/tests/test_150_questions_round_22/test_29_3sum_round_22.py @@ -0,0 +1,25 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_29_3sum import Solution + +class ThreeSumTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.threeSum(nums = [-1,0,1,2,-1,-4]) + target = [(-1,-1,2),(-1,0,1)] + output.sort() + target.sort() + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.threeSum(nums = [0,1,1]) + target = [] + self.assertEqual(output, target) + + def test_third_pattern(self): + solution = Solution() + output = solution.threeSum(nums = [0,0,0]) + target = [(0,0,0)] + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_30_minimum_size_subarray_sum_round_22.py b/tests/test_150_questions_round_22/test_30_minimum_size_subarray_sum_round_22.py new file mode 100644 index 00000000..76ae833c --- /dev/null +++ b/tests/test_150_questions_round_22/test_30_minimum_size_subarray_sum_round_22.py @@ -0,0 +1,23 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_30_minimum_size_subarray_sum import Solution + +class MinimumSizeSubarraySumTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.minSubArrayLen(target = 7, nums = [2,3,1,2,4,3]) + target = 2 + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.minSubArrayLen(target = 4, nums = [1,4,4]) + target = 1 + self.assertEqual(output, target) + + def test_third_pattern(self): + solution = Solution() + output = solution.minSubArrayLen(target = 11, nums = [1,1,1,1,1,1,1,1]) + target = 0 + self.assertEqual(output, target)