Skip to content

Commit 33a18df

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 11, 15 and 167
1 parent 6e87cbc commit 33a18df

7 files changed

+155
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
2828
- [2 Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/)
2929
- [6 Zigzag Conversion](https://leetcode.com/problems/zigzag-conversion/description/)
3030
- [9 Palindrome Number](https://leetcode.com/problems/palindrome-number/description/)
31+
- [11 Container With Most Water](https://leetcode.com/problems/container-with-most-water/description/)
3132
- [12 Integer to Roman](https://leetcode.com/problems/integer-to-roman/description/)
3233
- [13 Roman to Integer](https://leetcode.com/problems/roman-to-integer/description/)
3334
- [14 Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/description/)
35+
- [15 3Sum](https://leetcode.com/problems/3sum/description/)
3436
- [19 Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/)
3537
- [20 Valid Parentheses](https://leetcode.com/problems/valid-parentheses/description/)
3638
- [21 Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/)
@@ -88,6 +90,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
8890
- [150 Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/description/)
8991
- [151 Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/description/)
9092
- [155 Min Stack](https://leetcode.com/problems/min-stack/description/)
93+
- [167 Two Sum II - Input Array Is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/)
9194
- [169 Majority Element](https://leetcode.com/problems/majority-element/description/)
9295
- [172 Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/description/)
9396
- [173 Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/description/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def maxArea(self, height: List[int]) -> int:
8+
"""
9+
You are given an integer array height of length n. There are n vertical lines
10+
drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
11+
12+
Find two lines that together with the x-axis form a container, such that the
13+
container contains the most water.
14+
15+
Return the maximum amount of water a container can store.
16+
17+
Notice that you may not slant the container.
18+
"""
19+
maxArea = 0
20+
left, right = 0, len(height) - 1
21+
while left < right:
22+
maxArea = max(maxArea, min(height[left], height[right]) * (right - left))
23+
if height[left] < height[right]:
24+
left += 1
25+
else:
26+
right -= 1
27+
return maxArea

awesome_python_leetcode/_15_3sum.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def threeSum(self, nums: List[int]) -> List[List[int]]:
8+
"""
9+
Given an integer array nums, return all the triplets
10+
[nums[i], nums[j],nums[k]] such that i != j, i != k, and j != k, and
11+
nums[i] + nums[j] + nums[k] == 0.
12+
13+
Notice that the solution set must not contain duplicate triplets.
14+
"""
15+
nums = sorted(nums)
16+
threeSums = []
17+
i = 0
18+
while i < len(nums) - 2:
19+
if i > 0 and nums[i] == nums[i - 1]:
20+
i += 1
21+
continue
22+
left, right = i + 1, len(nums) - 1
23+
while left < right:
24+
threeSum = nums[i] + nums[left] + nums[right]
25+
if threeSum > 0:
26+
right -= 1
27+
elif threeSum < 0:
28+
left += 1
29+
else:
30+
threeSums.append([nums[i], nums[left], nums[right]])
31+
left += 1
32+
while left < right and nums[left] == nums[left - 1]:
33+
left += 1
34+
i += 1
35+
return threeSums
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def twoSum(self, numbers: List[int], target: int) -> List[int]:
8+
"""
9+
Given a 1-indexed array of integers numbers that is already sorted in
10+
non-decreasing order, find two numbers such that they add up to a specific
11+
target number. Let these two numbers be numbers[index1] and numbers[index2]
12+
where 1 <= index1 < index2 <= numbers.length.
13+
14+
Return the indices of the two numbers, index1 and index2, added by one as an
15+
integer array [index1, index2] of length 2.
16+
17+
The tests are generated such that there is exactly one solution. You may not
18+
use the same element twice.
19+
20+
Your solution must use only constant extra space.
21+
"""
22+
if len(numbers) == 2:
23+
return [1, 2]
24+
25+
left, right = 0, len(numbers) - 1
26+
while left < right:
27+
twosum = numbers[left] + numbers[right]
28+
if twosum < target:
29+
left += 1
30+
elif twosum > target:
31+
right -= 1
32+
else:
33+
return [left + 1, right + 1]
34+
return [left + 1, right + 1]
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._11_container_with_most_water import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["height", "expected"],
10+
argvalues=[
11+
([1, 8, 6, 2, 5, 4, 8, 3, 7], 49),
12+
([1, 1], 1),
13+
],
14+
)
15+
def test_func(height: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
17+
max_area = Solution().maxArea(height)
18+
assert max_area == expected

tests/test_15_3sum.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._15_3sum import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([-1, 0, 1, 2, -1, -4], [[-1, -1, 2], [-1, 0, 1]]),
12+
([0, 1, 1], []),
13+
([0, 0, 0], [[0, 0, 0]]),
14+
],
15+
)
16+
def test_func(nums: List[int], expected: List[List[int]]):
17+
"""Tests the solution of a LeetCode problem."""
18+
three_sum = Solution().threeSum(nums)
19+
assert three_sum == expected
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._167_two_sum_II_input_array_is_sorted import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["numbers", "target", "expected"],
10+
argvalues=[
11+
([2, 7, 11, 15], 9, [1, 2]),
12+
([2, 3, 4], 6, [1, 3]),
13+
([-1, 0], -1, [1, 2]),
14+
],
15+
)
16+
def test_func(numbers: List[int], target: int, expected: List[int]):
17+
"""Tests the solution of a LeetCode problem."""
18+
two_sum = Solution().twoSum(numbers, target)
19+
assert two_sum == expected

0 commit comments

Comments
 (0)