Skip to content

Commit 30aa199

Browse files
author
Navy.Tian
committed
add weekly-contest 122d
1 parent aacc968 commit 30aa199

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

weekly-contest/122d/p1_3009.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def minimumCost(self, nums: List[int]) -> int:
6+
ans = nums[0]
7+
nums = nums[1:]
8+
x1 = min(nums)
9+
nums.remove(x1)
10+
ans += x1
11+
x2 = min(nums)
12+
ans += x2
13+
nums.remove(x2)
14+
return ans
15+
16+
17+
if __name__ == "__main__":
18+
assert Solution().minimumCost(nums=[1, 2, 3, 12]) == 6
19+
assert Solution().minimumCost(nums=[10, 3, 1, 1]) == 12
20+
assert Solution().minimumCost(nums=[5, 4, 3]) == 12

weekly-contest/122d/p2_3010.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import List, Optional
2+
3+
4+
class Solution:
5+
def canSortArray(self, nums: List[int]) -> bool:
6+
return self._can_up_order(nums)
7+
8+
# def _can_down_order(self, nums: Optional[List[int]] = None) -> bool:
9+
# if nums is None:
10+
# nums = []
11+
# for i in range(0, len(nums)):
12+
# for j in range(i + 1, len(nums)):
13+
# if nums[i] < nums[j]:
14+
# if self._has_equal_one_bit(nums[i], nums[j]):
15+
# nums[i], nums[j] = nums[j], nums[i]
16+
# else:
17+
# return False
18+
# return True
19+
20+
def _can_up_order(self, nums: Optional[List[int]] = None) -> bool:
21+
if nums is None:
22+
nums = []
23+
for i in range(0, len(nums)):
24+
for j in range(i + 1, len(nums)):
25+
if nums[i] > nums[j]:
26+
if self._has_equal_one_bit(nums[i], nums[j]):
27+
nums[i], nums[j] = nums[j], nums[i]
28+
else:
29+
return False
30+
return True
31+
32+
def _has_equal_one_bit(self, x: int, y: int) -> bool:
33+
return self._get_one_bit_count(x) == self._get_one_bit_count(y)
34+
35+
def _get_one_bit_count(self, number: int) -> int:
36+
cnt = 0
37+
while number:
38+
if number & 1:
39+
cnt += 1
40+
number >>= 1
41+
return cnt
42+
43+
44+
if __name__ == "__main__":
45+
assert Solution().canSortArray(nums=[8, 4, 2, 30, 15]) is True
46+
assert Solution().canSortArray(nums=[1, 2, 3, 4, 5]) is True
47+
assert Solution().canSortArray(nums=[3, 16, 8, 4, 2]) is False

weekly-contest/122d/p3_3011.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def minimumArrayLength(self, nums: List[int]) -> int:
6+
# TODO: timeout
7+
nums.sort()
8+
temp_set = set(nums)
9+
zero = 0
10+
while True and len(nums) > 1:
11+
maxs = nums[-1]
12+
if maxs == 0:
13+
zero += 1
14+
nums.remove(maxs)
15+
mins = nums[0]
16+
if mins == 0:
17+
zero += 1
18+
nums.remove(mins)
19+
if maxs > 0 and mins > 0:
20+
maybe1 = mins % maxs
21+
maybe2 = maxs % mins
22+
if mins % maxs == 0:
23+
zero += 1
24+
nums = nums[1:-1]
25+
elif maybe2 != 0 and maybe2 not in temp_set:
26+
nums = [maybe2] + nums[1:-1]
27+
temp_set.add(maybe2)
28+
else:
29+
nums = [maybe1] + nums[1:-1]
30+
temp_set.add(maybe1)
31+
return len(nums) + zero
32+
33+
34+
if __name__ == "__main__":
35+
assert Solution().minimumArrayLength(nums=[1, 4, 3, 1]) == 1
36+
assert Solution().minimumArrayLength(nums=[5, 5, 5, 10, 5]) == 2
37+
assert Solution().minimumArrayLength(nums=[2, 3, 4]) == 1
38+
assert Solution().minimumArrayLength(nums=[3, 3, 1]) == 1
39+
assert Solution().minimumArrayLength(nums=[0, 0, 0]) == 3
40+
assert Solution().minimumArrayLength(nums=[0, 0, 1]) == 3
41+
assert Solution().minimumArrayLength(nums=[5, 2, 2, 2, 9, 10]) == 1

weekly-contest/122d/p4_3012.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
https://leetcode.cn/problems/divide-an-array-into-subarrays-with-minimum-cost-ii/description/
3+
"""
4+
from typing import List
5+
6+
7+
class Solution:
8+
def minimumCost(self, nums: List[int], k: int, dist: int) -> int:
9+
# TODO
10+
...
11+
12+
13+
if __name__ == "__main__":
14+
assert Solution().minimumCost(nums=[1, 3, 2, 6, 4, 2], k=3, dist=3) == 5
15+
assert Solution().minimumCost(nums=[10, 1, 2, 2, 2, 1], k=4, dist=3) == 15
16+
assert Solution().minimumCost(nums=[10, 8, 18, 9], k=3, dist=1) == 36

0 commit comments

Comments
 (0)