Skip to content

Commit 001640c

Browse files
committed
D. J.:
- Aded the leetcode problem and solution for 75
1 parent 6b9e778 commit 001640c

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
- [72 Edit Distance](https://leetcode.com/problems/edit-distance/description/)
117117
- [73 Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/description/)
118118
- [74 Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/description/)
119+
- [75 Sort Colors](https://leetcode.com/problems/sort-colors/description/)
119120
- [76 Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/description/)
120121
- [77 Combinations](https://leetcode.com/problems/combinations/description/)
121122
- [79 Word Search](https://leetcode.com/problems/word-search/description/)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def sortColorsBS(self, nums: List[int]) -> None:
8+
"""
9+
Given an array nums with n objects colored red, white, or blue, sort them
10+
in-place so that objects of the same color are adjacent, with the colors in the
11+
order red, white, and blue.
12+
13+
We will use the integers 0, 1, and 2 to represent the color red, white, and
14+
blue, respectively.
15+
16+
You must solve this problem without using the library's sort function.
17+
18+
Do not return anything, modify nums in-place instead.
19+
"""
20+
# Bucket Sort Solution
21+
# Time Complexity: O(n)
22+
# Space Complexity: O(1)
23+
24+
# Count the number of occurrences of each color
25+
count = [0, 0, 0]
26+
for i in range(len(nums)):
27+
count[nums[i]] += 1
28+
29+
# Fill the array with the sorted colors
30+
j = 0
31+
for i in range(len(nums)):
32+
while j < len(count) and count[j] == 0:
33+
j += 1
34+
nums[i] = j
35+
count[j] -= 1
36+
37+
def sortColorsQS(self, nums: List[int]) -> None:
38+
"""
39+
Given an array nums with n objects colored red, white, or blue, sort them
40+
in-place so that objects of the same color are adjacent, with the colors in the
41+
order red, white, and blue.
42+
43+
We will use the integers 0, 1, and 2 to represent the color red, white, and
44+
blue, respectively.
45+
46+
You must solve this problem without using the library's sort function.
47+
48+
Do not return anything, modify nums in-place instead.
49+
"""
50+
# (Simple) Quick Sort Solution
51+
# Time Complexity: O(n)
52+
# Space Complexity: O(1)
53+
54+
left, right = 0, len(nums) - 1
55+
i = 0
56+
57+
while i <= right:
58+
if nums[i] == 0:
59+
nums[left], nums[i] = nums[i], nums[left]
60+
left += 1
61+
elif nums[i] == 2:
62+
nums[i], nums[right] = nums[right], nums[i]
63+
right -= 1
64+
i -= 1
65+
i += 1

tests/test_75_sort_colors.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._75_sort_colors import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([2, 0, 2, 1, 1, 0], [0, 0, 1, 1, 2, 2]),
12+
([2, 0, 1], [0, 1, 2]),
13+
],
14+
)
15+
def test_func_bs(nums: List[int], expected: List[int]):
16+
"""Tests the solution of a LeetCode problem."""
17+
Solution().sortColorsBS(nums)
18+
assert nums == expected
19+
20+
21+
@pytest.mark.parametrize(
22+
argnames=["nums", "expected"],
23+
argvalues=[
24+
([2, 0, 2, 1, 1, 0], [0, 0, 1, 1, 2, 2]),
25+
([2, 0, 1], [0, 1, 2]),
26+
],
27+
)
28+
def test_func_qs(nums: List[int], expected: List[int]):
29+
"""Tests the solution of a LeetCode problem."""
30+
Solution().sortColorsQS(nums)
31+
assert nums == expected

0 commit comments

Comments
 (0)