[백준(python)] 10819번 : 차이를 최대로

hodu·2022년 11월 2일
0

algorithm

목록 보기
24/27

[백준(python)] 10819번 : 차이를 최대로
https://www.acmicpc.net/problem/10819

브루스포트, 백트래킹을 이용하는 문제인데 순열로 풀려서 순열로 풀었다.

N = int(input())
A = list(map(int,input().split()))

from itertools import permutations

# 1. 순열을 이용하기
result = 0
for p in permutations(A):
    temp = 0

    for i in range(N-1):
        temp += abs(p[i]-p[i-1])

    result = max(result, temp)

print(result)

코드를 간단히 설명하자면

  1. 모든 순열을 뽑는다.
    (여기서 10, 1, 2, 31, 10, 2, 3은 다른 의미이기 때문에 순열을 이용해야 한다.)

  2. 앞뒤로 더해준다.

  3. 가장 큰 결과값을 출력한다.

profile
안녕 세계!

0개의 댓글