Skip to content

Commit 0e3b377

Browse files
author
Navy.Tian
committed
add weekly-contest 381
1 parent 30aa199 commit 0e3b377

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

weekly-contest/381/p1_100191.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from collections import Counter
2+
3+
4+
class Solution:
5+
def minimumPushes(self, word: str) -> int:
6+
counter = Counter(word)
7+
times = 1
8+
numbers = 0
9+
ans = 0
10+
while len(counter):
11+
char, count = counter.most_common(1)[0]
12+
ans += count * times
13+
numbers += 1
14+
if numbers == 8:
15+
times += 1
16+
numbers = 0
17+
counter.pop(char)
18+
return ans
19+
20+
21+
if __name__ == "__main__":
22+
assert Solution().minimumPushes(word="abcde") == 5
23+
assert Solution().minimumPushes(word="xycdefghij") == 12

weekly-contest/381/p2_100188.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from collections import Counter
2+
from typing import List
3+
4+
5+
class Solution:
6+
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
7+
distances = [[n * 2 + 1 for _ in range(n)] for _ in range(n)]
8+
for i in range(n):
9+
if i + 1 < n:
10+
distances[i][i + 1] = 1
11+
if i - 1 >= 0:
12+
distances[i][i - 1] = 1
13+
if x != y:
14+
distances[x - 1][y - 1] = 1
15+
distances[y - 1][x - 1] = 1
16+
for i in range(len(distances)):
17+
for j in range(len(distances)):
18+
for k in range(len(distances[j])):
19+
if i != j != k and distances[i][j] > distances[i][k] + distances[k][j]:
20+
distances[i][j] = distances[i][k] + distances[k][j]
21+
for i in range(len(distances)):
22+
for j in range(len(distances)):
23+
for k in range(len(distances[j])):
24+
if i != j != k and distances[i][j] > distances[i][k] + distances[k][j]:
25+
distances[i][j] = distances[i][k] + distances[k][j]
26+
# TODO, timeout
27+
counter = Counter()
28+
for i in range(n):
29+
for j in range(n):
30+
if i != j:
31+
counter[distances[i][j]] += 1
32+
ans = []
33+
for i in range(n):
34+
ans.append(counter[i + 1])
35+
return ans
36+
37+
38+
if __name__ == "__main__":
39+
assert Solution().countOfPairs(n=3, x=1, y=3) == [6, 0, 0]
40+
assert Solution().countOfPairs(n=4, x=1, y=1) == [6, 4, 2, 0]
41+
assert Solution().countOfPairs(n=5, x=2, y=4) == [10, 8, 2, 0, 0]
42+
assert Solution().countOfPairs(n=6, x=2, y=6) == [12, 14, 4, 0, 0, 0]
43+
assert Solution().countOfPairs(n=1, x=1, y=1) == [0]

weekly-contest/381/p3_100192.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections import Counter
2+
3+
4+
class Solution:
5+
def minimumPushes(self, word: str) -> int:
6+
counter = Counter(word)
7+
times = 1
8+
numbers = 0
9+
ans = 0
10+
while len(counter):
11+
char, count = counter.most_common(1)[0]
12+
ans += count * times
13+
numbers += 1
14+
if numbers == 8:
15+
times += 1
16+
numbers = 0
17+
counter.pop(char)
18+
return ans
19+
20+
21+
if __name__ == "__main__":
22+
assert Solution().minimumPushes(word="abcde") == 5
23+
assert Solution().minimumPushes(word="xycdefghij") == 12
24+
assert Solution().minimumPushes(word="aabbccddeeffgghhiiiiii") == 24

weekly-contest/381/p4_100213.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def countOfPairs(self, n: int, x: int, y: int) -> List[int]:
6+
# TODO
7+
# distances = {}
8+
# for i in range(1, n + 1):
9+
# for j in range(i + 1, n + 1):
10+
# distances[(i, j)] = j - i
11+
# distances[(j, i)] = j - i
12+
# if x != y:
13+
# for i in range(y, n + 1):
14+
# distances[(x, i)] = 1 + (i - y)
15+
# distances[(i, x)] = 1 + (i - y)
16+
# for i in range(x, 0, -1):
17+
# distances[(y, i)] = 1 + (x - i)
18+
# distances[(i, y)] = 1 + (x - i)
19+
# print("[/Users/hj.tian/github/leetcode/weekly-contest/381/p2_100188.py:10] distances: ", distances)
20+
# ans = []
21+
# for i in range(1, n + 1):
22+
# count = sum(1 if value == i else 0 for value in distances.values())
23+
# ans.append(count)
24+
# print("[/Users/hj.tian/github/leetcode/weekly-contest/381/p2_100188.py:18] ans: ", ans)
25+
# return ans
26+
27+
28+
if __name__ == "__main__":
29+
assert Solution().countOfPairs(n=3, x=1, y=3) == [6, 0, 0]
30+
assert Solution().countOfPairs(n=4, x=1, y=1) == [6, 4, 2, 0]
31+
assert Solution().countOfPairs(n=5, x=2, y=4) == [10, 8, 2, 0, 0]
32+
assert Solution().countOfPairs(n=6, x=2, y=6) == [12, 14, 4, 0, 0, 0]
33+
assert Solution().countOfPairs(n=1, x=1, y=1) == [0]

0 commit comments

Comments
 (0)