[Python] 알고리즘/Silver
[10819] 차이를 최대로 (Python)
-Becca-
2022. 4. 12. 11:47
[문제]
https://www.acmicpc.net/problem/10819
10819번: 차이를 최대로
첫째 줄에 N (3 ≤ N ≤ 8)이 주어진다. 둘째 줄에는 배열 A에 들어있는 정수가 주어진다. 배열에 들어있는 정수는 -100보다 크거나 같고, 100보다 작거나 같다.
www.acmicpc.net
알고리즘 분류는 브루트포스 알고리즘, 백트래킹 입니다.
저는 Python의 permutations (순열)을 사용하여 풀었습니다.
[코드]
import sys
from itertools import permutations
N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
perm = list(permutations(A, N))
max = -1
for i in perm:
res = 0
for j in range(N - 1):
res += abs(i[j] - i[j + 1])
if max < res:
max = res
print(max)