📍 문제 설명
💡 접근
순열문제이다.
👩💻 코드
import java.util.*;
public class Main {
static int n, m, sum, answer = Integer.MIN_VALUE;
static char[][] board,clone;
static char[] c;
static int[] arr,pm;
static boolean[] visited;
static int[] dx = {-1,1,0,0};
static int[] dy = {0,0,-1,1};
static ArrayList<String> list = new ArrayList<>();
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
Main T = new Main();
n = kb.nextInt();
arr = new int[n];
pm = new int[n];
visited = new boolean[n];
for(int i = 0; i < n; i++) {
arr[i] = kb.nextInt();
}
T.solution(0);
System.out.println(answer);
}
private void solution(int L) {
if(L == n) { //n = 6
sum = 0;
for(int i = 0; i <= n-2; i++) {
sum += Math.abs(pm[i] - pm[i+1]);
}
answer = Math.max(answer, sum);
}
else {
for(int i = 0; i < n; i++) {
if(!visited[i]) {
visited[i] = true;
pm[L] = arr[i];
solution(L+1);
visited[i] = false;
}
}
}
}
}