코테 공부

백준[1181] 단어정렬 (파이썬 풀이)

moonsun623 2021. 10. 3. 19:05
반응형

https://www.acmicpc.net/problem/10819

 

10819번: 차이를 최대로

첫째 줄에 N (3 ≤ N ≤ 8)이 주어진다. 둘째 줄에는 배열 A에 들어있는 정수가 주어진다. 배열에 들어있는 정수는 -100보다 크거나 같고, 100보다 작거나 같다.

www.acmicpc.net

 

내가 한 풀이

from itertools import permutations
n=int(input())
nums=list(map(int,input().split(" ")))
answer=0
per=permutations(nums)
for num in per:
    tmp=0
    for j in range(len(num)-1):
        tmp+=abs(num[j]-num[j+1])
    if tmp>answer:
        answer=tmp
print(answer)

permutations()함수를 이용해 가능한 순열 리스트를 반환받는다.

for문를 돌려 모든 순열 리스트의 합을 비교하여 가장 높은 합을 찾는다.