From c22a393d7087b56ce17a5c126903e71051bf4072 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 12 Dec 2025 04:21:56 -0600 Subject: [PATCH] adding algo --- .../ex_27_two_sum_ii.py | 16 +++++++++++++ .../ex_28_container_with_most_water.py | 23 +++++++++++++++++++ .../test_27_two_sum_ii_round_22.py | 18 +++++++++++++++ ...t_28_container_with_most_water_round_22.py | 17 ++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_27_two_sum_ii.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_28_container_with_most_water.py create mode 100644 tests/test_150_questions_round_22/test_27_two_sum_ii_round_22.py create mode 100644 tests/test_150_questions_round_22/test_28_container_with_most_water_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_27_two_sum_ii.py b/src/my_project/interviews/top_150_questions_round_22/ex_27_two_sum_ii.py new file mode 100644 index 00000000..27a4db86 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_27_two_sum_ii.py @@ -0,0 +1,16 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def twoSum(self, numbers: List[int], target: int) -> List[int]: + + answer = dict() + + for k, v in enumerate(numbers): + + if v in answer: + return [answer[v]+1, k+1] + else: + answer[target - v] = k + + return [] \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_28_container_with_most_water.py b/src/my_project/interviews/top_150_questions_round_22/ex_28_container_with_most_water.py new file mode 100644 index 00000000..18d33ef2 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_28_container_with_most_water.py @@ -0,0 +1,23 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def maxArea(self, height: List[int]) -> int: + left = 0 + right = len(height) - 1 + max_area = 0 + + while left < right: + # Calculate current area + width = right - left + current_height = min(height[left], height[right]) + current_area = width * current_height + max_area = max(max_area, current_area) + + # Move pointer with shorter height + if height[left] < height[right]: + left += 1 + else: + right -= 1 + + return max_area \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_27_two_sum_ii_round_22.py b/tests/test_150_questions_round_22/test_27_two_sum_ii_round_22.py new file mode 100644 index 00000000..093dd84d --- /dev/null +++ b/tests/test_150_questions_round_22/test_27_two_sum_ii_round_22.py @@ -0,0 +1,18 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_27_two_sum_ii import Solution + +class TwoSumIIestCase(unittest.TestCase): + + def test_is_two_sum(self): + solution = Solution() + output = solution.twoSum(numbers = [2,7,11,15], target = 9) + target = [1,2] + for k, v in enumerate(target): + self.assertEqual(v, output[k]) + + def test_is_no_two_sum(self): + solution = Solution() + output = solution.twoSum(numbers=[2,7,11,15], target=0) + target = [] + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_28_container_with_most_water_round_22.py b/tests/test_150_questions_round_22/test_28_container_with_most_water_round_22.py new file mode 100644 index 00000000..029bc23d --- /dev/null +++ b/tests/test_150_questions_round_22/test_28_container_with_most_water_round_22.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_28_container_with_most_water import Solution + +class ContainerWithMostWaterTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.maxArea(height = [1,8,6,2,5,4,8,3,7]) + target = 49 + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.maxArea(height = [1,1]) + target = 1 + self.assertEqual(output, target) \ No newline at end of file