Skip to content

Commit b53cecb

Browse files
author
Navy.Tian
committed
add double 121 weekly-contest
1 parent 77e99d3 commit b53cecb

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

weekly-contest/121d/p1_2996.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""https://leetcode.cn/problems/smallest-missing-integer-greater-than-sequential-prefix-sum/"""
6+
7+
def missingInteger(self, nums: List[int]) -> int:
8+
prefix_sum = nums[0]
9+
for i in range(1, len(nums)):
10+
if nums[i] == nums[i - 1] + 1:
11+
prefix_sum += nums[i]
12+
else:
13+
break
14+
temp = set(nums)
15+
while prefix_sum in temp:
16+
prefix_sum += 1
17+
return prefix_sum
18+
19+
20+
if __name__ == "__main__":
21+
assert Solution().missingInteger([1, 2, 3, 2, 5]) == 6
22+
assert Solution().missingInteger([3, 4, 5, 1, 12, 14, 13]) == 15

weekly-contest/121d/p2_2997.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from functools import reduce
2+
from typing import List
3+
4+
5+
class Solution:
6+
def minOperations(self, nums: List[int], k: int) -> int:
7+
value = reduce(lambda x, y: x ^ y, nums) ^ k
8+
count = 0
9+
while value:
10+
if value & 1:
11+
count += 1
12+
value >>= 1
13+
return count
14+
15+
16+
if __name__ == "__main__":
17+
assert Solution().minOperations([2, 1, 3, 4], 1) == 2
18+
assert Solution().minOperations([2, 0, 2, 0], 0) == 0
19+
assert Solution().minOperations([4], 7) == 2

weekly-contest/121d/p3_2998.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import List, Tuple
2+
3+
4+
class Solution:
5+
def minimumOperationsToMakeEqual(self, x: int, y: int) -> int:
6+
queue: List[Tuple[int, int]] = [(x, 0)]
7+
uniques = set()
8+
while queue:
9+
cur_value, cur_cnt = queue[0]
10+
values: List[int] = [cur_value]
11+
for i in range(1, len(queue)):
12+
value, cnt = queue[i]
13+
if cnt == cur_cnt:
14+
values.append(value)
15+
else:
16+
break
17+
for value in values:
18+
if value == y:
19+
return cur_cnt
20+
else:
21+
if value > y:
22+
if value - 1 not in uniques:
23+
queue.append((value - 1, cur_cnt + 1))
24+
uniques.add(value - 1)
25+
if value % 11 == 0 and value // 11 not in uniques:
26+
queue.append((value // 11, cur_cnt + 1))
27+
uniques.add(value // 11)
28+
if value % 5 == 0 and value // 5 not in uniques:
29+
queue.append((value // 5, cur_cnt + 1))
30+
uniques.add(value // 5)
31+
if value + 1 not in uniques:
32+
queue.append((value + 1, cur_cnt + 1))
33+
uniques.add(value + 1)
34+
queue = queue[len(values) :]
35+
return -1
36+
37+
38+
if __name__ == "__main__":
39+
assert Solution().minimumOperationsToMakeEqual(26, 1) == 3
40+
assert Solution().minimumOperationsToMakeEqual(54, 2) == 4
41+
assert Solution().minimumOperationsToMakeEqual(25, 30) == 5
42+
assert Solution().minimumOperationsToMakeEqual(1, 16) == 15
43+
assert Solution().minimumOperationsToMakeEqual(89, 57) == 32

weekly-contest/121d/p4_2999.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int:
3+
# TODO: 未完成
4+
# 枚举会超时
5+
# 组合与排列,但如何排队最高位与 limit 相同但又不在 finish 之内的数呢?似乎数量级也在 10**14 会超时
6+
...
7+
8+
9+
if __name__ == "__main__":
10+
assert Solution().numberOfPowerfulInt(start=1, finish=6000, limit=4, s="124") == 5
11+
assert Solution().numberOfPowerfulInt(start=15, finish=215, limit=6, s="10") == 2
12+
assert Solution().numberOfPowerfulInt(start=1000, finish=2000, limit=4, s="3000") == 0

0 commit comments

Comments
 (0)