https://www.acmicpc.net/problem/10819
접근 : 브루트포스
전체적으로 배열 정리 후 규칙을 찾아보려고 했으나 특별한 규칙이 보이지 않아 순열을 통한 브루트포스로 문제풀이
/**
* 제출용. 아래 로컬용을 지우고 제출하자.
*/
// let input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n")
/**
* 로컬용, 예제.txt를 생성해서 예제를 복붙하자.
*/
let input = require("fs")
.readFileSync("input.txt") //"/dev/stdin"
.toString()
.split("\n")
.map((val) => val.trim());
function solution(input) {
let [T,...Array] = input;
Array = Array[0].split(" ").map((v)=>+v);
const permutations = getPermutations(Array,T)
let max = -99999;
permutations.forEach((v)=>{
let temp =0;
for(let i =0;i<=T-2;i++){
temp += Math.abs(v[i]-v[i+1]);
if(i===T-2&&temp>max)max=temp;
}
})
console.log(max);
}
// 순열
const getPermutations = function (arr, selectNumber) {
const results = [];
if (selectNumber === 1) return arr.map((el) => [el]);
// n개중에서 1개 선택할 때(nP1), 바로 모든 배열의 원소 return. 1개선택이므로 순서가 의미없음.
arr.forEach((fixed, index, origin) => {
const rest = [...origin.slice(0, index), ...origin.slice(index+1)]
// 해당하는 fixed를 제외한 나머지 배열
const permutations = getPermutations(rest, selectNumber - 1);
// 나머지에 대해서 순열을 구한다.
const attached = permutations.map((el) => [fixed, ...el]);
// 돌아온 순열에 떼 놓은(fixed) 값 붙이기
results.push(...attached);
// 배열 spread syntax 로 모두다 push
});
return results; // 결과 담긴 results return
}
solution(input);
'코딩테스트(알고리즘) > baekjoon' 카테고리의 다른 글
[baekjoon] 3986 좋은 단어(javascript) (0) | 2023.01.06 |
---|---|
[baekjoon] 14889 스타트와 링크 (0) | 2022.11.09 |
[baekjoon] 4659 비밀번호 발음하기 (Javascript,C++) (0) | 2022.07.12 |
[baekjoon] 2910 빈도 정렬 (Javascript,C++) (0) | 2022.07.12 |
[baekjoon] 2583 영역 구하기 (Javascript,c++) (0) | 2022.07.11 |