https://school.programmers.co.kr/learn/courses/30/lessons/60062
왜요?
from itertools import combinations
def find_solution(n, weak, dist, ans):
for i in range(len(weak)):
check = False
sol = []
index = i
for j in dist:
while weak[index] in sol:
index = (index + 1) % len(weak)
for k in range(weak[index], weak[index] + j + 1):
if (k % n not in sol):
sol.append(k % n)
if all(w in sol for w in weak):
check = True
break
if check == True:
ans.append(dist)
break
def solution(n, weak, dist):
dist_comb = []
ans = []
for i in range(1, len(dist) + 1):
comb = combinations(dist, i)
dist_comb.extend(comb)
for dist in dist_comb:
find_solution(n, weak, dist, ans)
if not ans:
return -1
else:
return len(ans[0])
순열을 계산하지 않음
n = 16
weak = [1,2,3,4,5,7,8,10,11,12,14,15]
dist = [4,2,1,1]
누가 이기나 해보자
from itertools import combinations, permutations
def find_solution(n, weak, dist, ans):
for i in range(len(weak)):
sol = set()
index = i
for j in dist:
while weak[index] in sol:
index = (index + 1) % len(weak)
for k in range(weak[index], weak[index] + j + 1):
sol.add(k % n)
if all(x in sol for x in weak):
ans.append(dist)
return True
return False
def solution(n, weak, dist):
ans = []
for i in range(1, len(dist) + 1):
comb = list(combinations(dist, i))
perm = set()
for j in comb:
if i == 1:
perm.add(j)
else:
perm.update(list(permutations(j, i)))
for j in perm:
if (find_solution(n, weak, j, ans)):
return i
return -1
효율성도 엄청 좋아졌다