-
Notifications
You must be signed in to change notification settings - Fork 0
/
comb_permu.py
48 lines (40 loc) · 1.21 KB
/
comb_permu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
result = []
def solution(orders, course):
global result
answer = []
for t in course:
dic = {}
for arr in orders:
visited = [False] * (len(orders[0]))
com(0, 0, sorted(list(arr)), visited, t)
for x in result:
if dic.get(x): dic[x] += 1
else: dic[x] = 1
result.clear()
temp = list(dic.items())
maxv = 2 # 2개 이상
candi = []
for x in temp:
if x[1] > maxv:
candi.clear()
candi.append(x[0])
maxv = x[1]
elif x[1] == maxv:
candi.append(x[0])
for y in candi: answer.append(y)
answer.sort()
return answer
def com(idx, depth, arr, visited, tar):
global result
if depth == tar:
comb = [arr[i] for i in range(len(arr)) if visited[i] == True]
result.append("".join(comb))
return
for i in range(idx, len(arr)):
if visited[i] == False:
visited[i] = True
com(i+1, depth+1, arr, visited, tar)
visited[i] = False
orders = ["XYZ", "ABCXY"]
course = [2, 3, 4]
print(solution(orders, course))