10819 - 차이를 최대로

LeeKyoungChang·2022년 4월 3일
0

Algorithm

목록 보기
89/203
post-thumbnail

📚 10819 - 차이를 최대로

차이를 최대로

 

이해

파이썬을 사용할 시, 순열과 조합을 라이브러리에서 호출하여 결과를 받을 수 있다.
문제에서 크기가 작으므로 조합함수 permutations을 이용하여 모든 경우의 수를 돌리면서, 가장 거리 계산이 큰 것을 답을 출력하면 된다.

 

소스

import sys
from itertools import permutations

read = sys.stdin.readline

n = int(read())

arr = list(map(int, read().split()))

# 순열로 조합한다.
cases = list(permutations(arr))

result = 0

for card in cases:
    ans = 0
    for idx in range(n - 1):
        ans += abs(card[idx] - card[idx + 1])
    result = max(result, ans)

print(result)

 

채점 결과

 

profile
"야, (오류 만났어?) 너두 (해결) 할 수 있어"

0개의 댓글