Skip to content

Commit b548a56

Browse files
authored
Merge pull request #1483 from ivan1016017/december12
adding algo
2 parents b39f74c + c22a393 commit b548a56

4 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def twoSum(self, numbers: List[int], target: int) -> List[int]:
6+
7+
answer = dict()
8+
9+
for k, v in enumerate(numbers):
10+
11+
if v in answer:
12+
return [answer[v]+1, k+1]
13+
else:
14+
answer[target - v] = k
15+
16+
return []
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def maxArea(self, height: List[int]) -> int:
6+
left = 0
7+
right = len(height) - 1
8+
max_area = 0
9+
10+
while left < right:
11+
# Calculate current area
12+
width = right - left
13+
current_height = min(height[left], height[right])
14+
current_area = width * current_height
15+
max_area = max(max_area, current_area)
16+
17+
# Move pointer with shorter height
18+
if height[left] < height[right]:
19+
left += 1
20+
else:
21+
right -= 1
22+
23+
return max_area
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_27_two_sum_ii import Solution
4+
5+
class TwoSumIIestCase(unittest.TestCase):
6+
7+
def test_is_two_sum(self):
8+
solution = Solution()
9+
output = solution.twoSum(numbers = [2,7,11,15], target = 9)
10+
target = [1,2]
11+
for k, v in enumerate(target):
12+
self.assertEqual(v, output[k])
13+
14+
def test_is_no_two_sum(self):
15+
solution = Solution()
16+
output = solution.twoSum(numbers=[2,7,11,15], target=0)
17+
target = []
18+
self.assertEqual(output, target)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_28_container_with_most_water import Solution
4+
5+
class ContainerWithMostWaterTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.maxArea(height = [1,8,6,2,5,4,8,3,7])
10+
target = 49
11+
self.assertEqual(output, target)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.maxArea(height = [1,1])
16+
target = 1
17+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)