Skip to content

Commit 404b278

Browse files
committed
daily
1 parent e47347c commit 404b278

File tree

25 files changed

+2950
-0
lines changed

25 files changed

+2950
-0
lines changed

problems/problem_0052/solution_1.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pytest
2+
3+
4+
class Solution:
5+
def totalNQueens(self, n: int) -> int:
6+
def dfs(c, rows):
7+
if c == n:
8+
return 1
9+
10+
res = 0
11+
for r in range(n):
12+
if all(r != x and abs(r - x) != abs(c - y) for y, x in enumerate(rows)):
13+
res += dfs(c + 1, rows + [r])
14+
15+
return res
16+
17+
return dfs(0, [])
18+
19+
20+
tests = [
21+
(
22+
(4,),
23+
2,
24+
),
25+
(
26+
(1,),
27+
1,
28+
),
29+
]
30+
31+
32+
@pytest.mark.timeout(2)
33+
@pytest.mark.parametrize(
34+
"inputs, expected",
35+
tests,
36+
)
37+
def test_validator(inputs, expected):
38+
output = Solution().totalNQueens(*inputs)
39+
assert output == expected

problems/problem_0054/solution_1.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pytest
2+
from typing import List
3+
import numpy as np
4+
5+
6+
class Solution:
7+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
8+
res = []
9+
matrix = np.asarray(matrix)
10+
11+
while matrix.size > 0:
12+
res.extend(matrix[0])
13+
matrix = np.rot90(matrix[1:])
14+
15+
return res
16+
17+
18+
tests = [
19+
(
20+
([[1, 2, 3], [4, 5, 6], [7, 8, 9]],),
21+
[1, 2, 3, 6, 9, 8, 7, 4, 5],
22+
),
23+
(
24+
([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],),
25+
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7],
26+
),
27+
]
28+
29+
30+
@pytest.mark.timeout(2)
31+
@pytest.mark.parametrize(
32+
"inputs, expected",
33+
tests,
34+
)
35+
def test_validator(inputs, expected):
36+
output = Solution().spiralOrder(*inputs)
37+
assert output == expected

problems/problem_0075/solution_1.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from collections import Counter
2+
from typing import List
3+
4+
import pytest
5+
6+
7+
class Solution:
8+
def sortColors(self, nums: List[int]) -> None:
9+
"""
10+
Do not return anything, modify nums in-place instead.
11+
"""
12+
count = Counter(nums)
13+
current = 0
14+
for i in range(len(nums)):
15+
while count[current] <= 0:
16+
current += 1
17+
18+
nums[i] = current
19+
count[current] -= 1
20+
21+
22+
tests = [
23+
(
24+
([2, 0, 2, 1, 1, 0],),
25+
[0, 0, 1, 1, 2, 2],
26+
),
27+
(
28+
([2, 0, 1],),
29+
[0, 1, 2],
30+
),
31+
]
32+
33+
34+
@pytest.mark.timeout(2)
35+
@pytest.mark.parametrize(
36+
"inputs, expected",
37+
tests,
38+
)
39+
def test_validator(inputs, expected):
40+
input = inputs[0]
41+
Solution().sortColors(input)
42+
assert input == expected

problems/problem_0075/solution_2.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
6+
class Solution:
7+
def sortColors(self, nums: List[int]) -> None:
8+
"""
9+
Do not return anything, modify nums in-place instead.
10+
"""
11+
red, white, blue = 0, 0, len(nums) - 1
12+
13+
while white <= blue:
14+
if nums[white] == 0:
15+
nums[red], nums[white] = nums[white], nums[red]
16+
red += 1
17+
white += 1
18+
elif nums[white] == 1:
19+
white += 1
20+
else:
21+
nums[white], nums[blue] = nums[blue], nums[white]
22+
blue -= 1
23+
24+
25+
tests = [
26+
(
27+
([2, 0, 2, 1, 1, 0],),
28+
[0, 0, 1, 1, 2, 2],
29+
),
30+
(
31+
([2, 0, 1],),
32+
[0, 1, 2],
33+
),
34+
]
35+
36+
37+
@pytest.mark.timeout(2)
38+
@pytest.mark.parametrize(
39+
"inputs, expected",
40+
tests,
41+
)
42+
def test_validator(inputs, expected):
43+
input = inputs[0]
44+
Solution().sortColors(input)
45+
assert input == expected

problems/problem_0260/solution_1.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from functools import reduce
2+
from typing import List
3+
4+
import pytest
5+
6+
7+
class Solution:
8+
def singleNumber(self, nums: List[int]) -> List[int]:
9+
xor = reduce(lambda x, y: x ^ y, nums)
10+
nz = xor & -xor
11+
res1 = reduce(lambda x, y: x ^ y, filter(lambda x: x & nz, nums))
12+
return [res1, res1 ^ xor]
13+
14+
15+
tests = [
16+
(
17+
([1, 2, 1, 3, 2, 5],),
18+
[3, 5],
19+
),
20+
(
21+
([-1, 0],),
22+
[-1, 0],
23+
),
24+
(
25+
([0, 1],),
26+
[1, 0],
27+
),
28+
]
29+
30+
31+
@pytest.mark.timeout(2)
32+
@pytest.mark.parametrize(
33+
"inputs, expected",
34+
tests,
35+
)
36+
def test_validator(inputs, expected):
37+
output = Solution().singleNumber(*inputs)
38+
assert sorted(output) == sorted(expected)

problems/problem_0330/solution_1.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from typing import List
2+
import pytest
3+
4+
5+
class Solution:
6+
def minPatches(self, nums: List[int], n: int) -> int:
7+
miss = 0
8+
res = 0
9+
i = 0
10+
while miss < n:
11+
x = nums[i] if i < len(nums) else n + 2
12+
if x > miss + 1:
13+
miss = miss * 2 + 1
14+
res += 1
15+
else:
16+
miss += x
17+
i += 1
18+
return res
19+
20+
21+
tests = [
22+
(
23+
([1, 3], 6),
24+
1,
25+
),
26+
(
27+
([1, 5, 10], 20),
28+
2,
29+
),
30+
(
31+
([1, 2, 2], 5),
32+
0,
33+
),
34+
(
35+
([1, 3], 2147483647),
36+
30,
37+
),
38+
]
39+
40+
41+
@pytest.mark.timeout(2)
42+
@pytest.mark.parametrize("inputs, expected", tests)
43+
def test_validator(inputs, expected):
44+
output = Solution().minPatches(*inputs)
45+
assert output == expected

problems/problem_0344/solution_1.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
from typing import List
3+
4+
5+
class Solution:
6+
def reverseString(self, s: List[str]) -> None:
7+
"""
8+
Do not return anything, modify s in-place instead.
9+
"""
10+
for i in range(len(s) // 2):
11+
s[i], s[-i - 1] = s[-i - 1], s[i]
12+
13+
14+
tests = [
15+
(
16+
(["h", "e", "l", "l", "o"],),
17+
["o", "l", "l", "e", "h"],
18+
),
19+
(
20+
(["H", "a", "n", "n", "a", "h"],),
21+
["h", "a", "n", "n", "a", "H"],
22+
),
23+
]
24+
25+
26+
@pytest.mark.timeout(2)
27+
@pytest.mark.parametrize(
28+
"inputs, expected",
29+
tests,
30+
)
31+
def test_validator(inputs, expected):
32+
input = inputs[0]
33+
Solution().reverseString(input)
34+
assert input == expected

0 commit comments

Comments
 (0)