[문제] 외벽 점검
https://programmers.co.kr/learn/courses/30/lessons/60062
문제해결 hint
weak = [1, 3, 4, 9, 10]
length = len(weak)
for i in range(length):
weak.append(weak[i] + n)
result
weak = [1, 3, 4, 9, 10, 13, 15, 16, 21, 22]
for friends in list(permutations(dist, len(dist))):
print(friends)
result
(3, 5, 7)
(3, 7, 5)
(5, 3, 7)
(5, 7, 3)
(7, 3, 5)
(7, 5, 3)
from itertools import permutations
n = 12
weak = [1, 3, 4, 9, 10]
dist = [3, 5, 7]
def solution(n, weak, dist):
length = len(weak)
for i in range(length):
weak.append(weak[i] + n)
# 투입할 친구 수 초기화
answer = len(dist) + 1
# 0부터 length - 1 까지의 위치(취약지점)를 각각 시작점으로 설정
for start in range(length):
for friends in list(permutations(dist, len(dist))):
# 투입할 친구의 수
count = 1
position = weak[start] + friends[count - 1]
# 시작지점부터 모든 취약지점을 확인
for index in range(start, start + length):
# 점검할 수 있는 위치를 벗어나는 경우
if position < weak[index]:
# 새로운 친구를 투입
count += 1
# 더 투입이 불가능하다면 종료
if count > len(dist):
break
position = weak[index] + friends[count - 1]
# 최소값 계산
answer = min(answer, count)
if answer > len(dist):
return -1
return answer
solution(n, weak, dist)
참고 [순열과 조합 - combinations, permutations]
https://programmers.co.kr/learn/courses/4008/lessons/12836