Skip to content

Commit 4da559f

Browse files
committed
adding algo
1 parent 66af6ee commit 4da559f

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from collections import defaultdict
3+
4+
class Solution:
5+
def countTheNumOfKFreeSubsets(self, nums: List[int], k: int) -> int:
6+
7+
groups = defaultdict(list)
8+
9+
for num in nums:
10+
groups[num % k].append(num)
11+
12+
res = 1
13+
14+
for group in groups.values():
15+
group.sort()
16+
17+
i = 0
18+
while i < len(group):
19+
chain = [group[i]]
20+
21+
j = i + 1
22+
23+
while j < len(group) and group[j] == chain[-1] + k:
24+
chain.append(group[j])
25+
j += 1
26+
27+
m = len(chain)
28+
if m == 1:
29+
chain_res = 2 # {} and {chain[0]}
30+
else:
31+
take = 1
32+
skip = 1
33+
34+
for i in range(1,m):
35+
new_take = skip
36+
new_skip = take + skip
37+
take, skip = new_take, new_skip
38+
39+
chain_res = take + skip
40+
41+
42+
res *= chain_res
43+
44+
i = j
45+
46+
return res
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
3+
class Solution:
4+
def reorderLogFiles(self, logs: List[str]) -> List[str]:
5+
6+
letter_logs = []
7+
digit_logs = []
8+
9+
for log in logs:
10+
_id, rest = log.split(' ', 1)
11+
if rest[0].isdigit():
12+
digit_logs.append(log)
13+
else:
14+
letter_logs.append((rest, _id, log))
15+
letter_logs.sort(key=lambda x: (x[0],x[1]))
16+
return [orig for _,_,orig in letter_logs] + digit_logs
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
3+
class Solution:
4+
def reorderLogFiles(self, logs: List[str]) -> List[str]:
5+
letter_logs = []
6+
digit_logs = []
7+
for log in logs:
8+
id_, rest = log.split(" ", 1)
9+
if rest[0].isdigit():
10+
digit_logs.append(log)
11+
else:
12+
letter_logs.append((rest, id_, log))
13+
# sort by content first, then identifier
14+
letter_logs.sort(key=lambda x: (x[0], x[1]))
15+
return [orig for _, _, orig in letter_logs] + digit_logs

0 commit comments

Comments
 (0)