Skip to content

Commit d868da9

Browse files
committed
daily
1 parent 2d3a541 commit d868da9

File tree

13 files changed

+612
-5
lines changed

13 files changed

+612
-5
lines changed

problems/problem_0874/solution_1.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
6+
class Solution:
7+
def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
8+
obstacles = set((x + y * 1j for x, y in obstacles))
9+
res = 0
10+
pos = 0
11+
direction = 1j
12+
for cmd in commands:
13+
if cmd == -1:
14+
direction *= -1j
15+
elif cmd == -2:
16+
direction *= 1j
17+
else:
18+
for _ in range(cmd):
19+
if pos + direction in obstacles:
20+
break
21+
pos += direction
22+
res = max(res, int(pos.real) ** 2 + int(pos.imag) ** 2)
23+
24+
return res
25+
26+
27+
tests = [
28+
(
29+
([4, -1, 3], []),
30+
25,
31+
),
32+
(
33+
([4, -1, 4, -2, 4], [[2, 4]]),
34+
65,
35+
),
36+
(
37+
([6, -1, -1, 6], []),
38+
36,
39+
),
40+
]
41+
42+
43+
@pytest.mark.timeout(2)
44+
@pytest.mark.parametrize(
45+
"inputs, expected",
46+
tests,
47+
)
48+
def test_validator(inputs, expected):
49+
output = Solution().robotSim(*inputs)
50+
assert output == expected

problems/problem_0947/solution_1.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from typing import List
2+
import pytest
3+
4+
5+
class Solution:
6+
def removeStones(self, stones: List[List[int]]) -> int:
7+
union_find = {}
8+
9+
def find(x):
10+
if x != union_find[x]:
11+
union_find[x] = find(union_find[x])
12+
return union_find[x]
13+
14+
def union(x, y):
15+
union_find.setdefault(x, x)
16+
union_find.setdefault(y, y)
17+
union_find[find(x)] = find(y)
18+
19+
for x, y in stones:
20+
union(x, ~y)
21+
22+
return len(stones) - len({find(x) for x in union_find})
23+
24+
25+
tests = [
26+
(
27+
([[0, 0], [0, 1], [1, 0], [1, 2], [2, 1], [2, 2]],),
28+
5,
29+
),
30+
(
31+
([[0, 0], [0, 2], [1, 1], [2, 0], [2, 2]],),
32+
3,
33+
),
34+
(
35+
([[0, 0]],),
36+
0,
37+
),
38+
(
39+
([[0, 1], [1, 0], [1, 1]],),
40+
2,
41+
),
42+
]
43+
44+
45+
@pytest.mark.timeout(2)
46+
@pytest.mark.parametrize("inputs, expected", tests)
47+
def test_validator(inputs, expected):
48+
output = Solution().removeStones(*inputs)
49+
assert output == expected

problems/problem_1367/solution_1.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from typing import Optional
2+
3+
import pytest
4+
5+
from problems.utils import ListNode
6+
from problems.utils import TreeNode
7+
8+
9+
class Solution:
10+
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
11+
def dfs(head, root):
12+
if not head:
13+
return True
14+
if not root:
15+
return False
16+
return head.val == root.val and (dfs(head.next, root.left) or dfs(head.next, root.right))
17+
18+
if not head:
19+
return True
20+
if not root:
21+
return False
22+
return dfs(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)
23+
24+
25+
tests = [
26+
(
27+
([4, 2, 8], [1, 4, 4, None, 2, 2, None, 1, None, 6, 8, None, None, None, None, 1, 3]),
28+
True,
29+
),
30+
(
31+
([1, 4, 2, 6], [1, 4, 4, None, 2, 2, None, 1, None, 6, 8, None, None, None, None, 1, 3]),
32+
True,
33+
),
34+
(
35+
([1, 4, 2, 6, 8], [1, 4, 4, None, 2, 2, None, 1, None, 6, 8, None, None, None, None, 1, 3]),
36+
False,
37+
),
38+
(
39+
([1, 10], [1, None, 1, 10, 1, 9]),
40+
True,
41+
),
42+
(
43+
([2, 2, 1], [2, None, 2, None, 2, None, 1]),
44+
True,
45+
),
46+
]
47+
48+
49+
@pytest.mark.timeout(2)
50+
@pytest.mark.parametrize("inputs, expected", tests)
51+
def test_validator(inputs, expected):
52+
output = Solution().isSubPath(ListNode.from_list(inputs[0]), TreeNode.from_list(inputs[1]))
53+
assert output == expected

problems/problem_1367/solution_2.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from typing import Optional
2+
3+
import pytest
4+
5+
from problems.utils import ListNode
6+
from problems.utils import TreeNode
7+
8+
9+
class Solution:
10+
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
11+
sub_path = []
12+
while head:
13+
sub_path.append(head.val)
14+
head = head.next
15+
16+
suffix_dp = [0] * len(sub_path)
17+
j = 0
18+
for i in range(1, len(sub_path)):
19+
while j and sub_path[i] != sub_path[j]:
20+
j = suffix_dp[j - 1]
21+
j += sub_path[i] == sub_path[j]
22+
suffix_dp[i] = j
23+
24+
def dfs(root, i):
25+
if not root:
26+
return False
27+
while i and root.val != sub_path[i]:
28+
i = suffix_dp[i - 1]
29+
i += root.val == sub_path[i]
30+
return i == len(suffix_dp) or dfs(root.left, i) or dfs(root.right, i)
31+
32+
return dfs(root, 0)
33+
34+
35+
tests = [
36+
(
37+
([4, 2, 8], [1, 4, 4, None, 2, 2, None, 1, None, 6, 8, None, None, None, None, 1, 3]),
38+
True,
39+
),
40+
(
41+
([1, 4, 2, 6], [1, 4, 4, None, 2, 2, None, 1, None, 6, 8, None, None, None, None, 1, 3]),
42+
True,
43+
),
44+
(
45+
([1, 4, 2, 6, 8], [1, 4, 4, None, 2, 2, None, 1, None, 6, 8, None, None, None, None, 1, 3]),
46+
False,
47+
),
48+
(
49+
([1, 10], [1, None, 1, 10, 1, 9]),
50+
True,
51+
),
52+
(
53+
([2, 2, 1], [2, None, 2, None, 2, None, 1]),
54+
True,
55+
),
56+
]
57+
58+
59+
@pytest.mark.timeout(2)
60+
@pytest.mark.parametrize("inputs, expected", tests)
61+
def test_validator(inputs, expected):
62+
output = Solution().isSubPath(ListNode.from_list(inputs[0]), TreeNode.from_list(inputs[1]))
63+
assert output == expected

problems/problem_1514/solution_1.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import heapq
2+
from collections import defaultdict
3+
from typing import List
4+
5+
import pytest
6+
7+
8+
class Solution:
9+
def maxProbability(
10+
self, n: int, edges: List[List[int]], succProb: List[float], start_node: int, end_node: int
11+
) -> float:
12+
graph = defaultdict(list)
13+
for (s, d), w in zip(edges, succProb):
14+
graph[s].append((w, d))
15+
graph[d].append((w, s))
16+
17+
pq = [(-1, start_node)]
18+
seen = set()
19+
20+
while pq:
21+
cost, current = heapq.heappop(pq)
22+
if current == end_node:
23+
return -cost
24+
25+
seen.add(current)
26+
for weight, neighbor in graph[current]:
27+
if neighbor not in seen:
28+
heapq.heappush(pq, (cost * weight, neighbor))
29+
30+
return 0
31+
32+
33+
tests = [
34+
(
35+
(3, [[0, 1], [1, 2], [0, 2]], [0.5, 0.5, 0.2], 0, 2),
36+
0.25000,
37+
),
38+
(
39+
(3, [[0, 1], [1, 2], [0, 2]], [0.5, 0.5, 0.3], 0, 2),
40+
0.30000,
41+
),
42+
(
43+
(3, [[0, 1]], [0.5], 0, 2),
44+
0.00000,
45+
),
46+
]
47+
48+
49+
@pytest.mark.timeout(2)
50+
@pytest.mark.parametrize(
51+
"inputs, expected",
52+
tests,
53+
)
54+
def test_validator(inputs, expected):
55+
output = Solution().maxProbability(*inputs)
56+
assert output == expected

problems/problem_1894/solution_1.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import bisect
2+
import pytest
3+
from typing import List
4+
5+
6+
class Solution:
7+
def chalkReplacer(self, chalk: List[int], k: int) -> int:
8+
cum_sum = [0] * len(chalk)
9+
cum_sum[0] = chalk[0]
10+
for i in range(1, len(chalk)):
11+
cum_sum[i] = cum_sum[i - 1] + chalk[i]
12+
k = k % cum_sum[-1]
13+
return bisect.bisect_right(cum_sum, k)
14+
15+
16+
tests = [
17+
(
18+
([5, 1, 5], 22),
19+
0,
20+
),
21+
(
22+
([3, 4, 1, 2], 25),
23+
1,
24+
),
25+
]
26+
27+
28+
@pytest.mark.timeout(2)
29+
@pytest.mark.parametrize("inputs, expected", tests)
30+
def test_validator(inputs, expected):
31+
output = Solution().chalkReplacer(*inputs)
32+
assert output == expected

problems/problem_1905/solution_1.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
6+
class Solution:
7+
def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:
8+
def dfs(i, j):
9+
if i < 0 or j < 0 or i >= len(grid2) or j >= len(grid2[0]) or grid2[i][j] == 0:
10+
return
11+
12+
grid2[i][j] = 0
13+
for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
14+
dfs(i + dx, j + dy)
15+
16+
for i in range(len(grid2)):
17+
for j in range(len(grid2[0])):
18+
if grid2[i][j] == 1 and grid1[i][j] == 0:
19+
dfs(i, j)
20+
21+
res = 0
22+
for i in range(len(grid2)):
23+
for j in range(len(grid2[0])):
24+
if grid2[i][j] == 1:
25+
res += 1
26+
dfs(i, j)
27+
28+
return res
29+
30+
31+
tests = [
32+
(
33+
(
34+
[[1, 1, 1, 0, 0], [0, 1, 1, 1, 1], [0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 0, 1, 1]],
35+
[[1, 1, 1, 0, 0], [0, 0, 1, 1, 1], [0, 1, 0, 0, 0], [1, 0, 1, 1, 0], [0, 1, 0, 1, 0]],
36+
),
37+
3,
38+
),
39+
(
40+
(
41+
[[1, 0, 1, 0, 1], [1, 1, 1, 1, 1], [0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [1, 0, 1, 0, 1]],
42+
[[0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [0, 1, 0, 1, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1]],
43+
),
44+
2,
45+
),
46+
]
47+
48+
49+
@pytest.mark.timeout(2)
50+
@pytest.mark.parametrize("inputs, expected", tests)
51+
def test_validator(inputs, expected):
52+
output = Solution().countSubIslands(*inputs)
53+
assert output == expected

problems/problem_1945/solution_1.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
3+
4+
class Solution:
5+
def getLucky(self, s: str, k: int) -> int:
6+
res = "".join(map(lambda x: str(ord(x) - 96), s))
7+
for _ in range(k):
8+
res = str(sum(map(int, res)))
9+
return int(res)
10+
11+
12+
tests = [
13+
(
14+
("iiii", 1),
15+
36,
16+
),
17+
(
18+
("leetcode", 2),
19+
6,
20+
),
21+
(
22+
("zbax", 2),
23+
8,
24+
),
25+
]
26+
27+
28+
@pytest.mark.timeout(2)
29+
@pytest.mark.parametrize(
30+
"inputs, expected",
31+
tests,
32+
)
33+
def test_validator(inputs, expected):
34+
output = Solution().getLucky(*inputs)
35+
assert output == expected

0 commit comments

Comments
 (0)