1부터 N까지의 수로 이루어진 순열이 있다. 이때, 사전순으로 다음에 오는 순열을 구하는 프로그램을 작성하시오.
사전 순으로 가장 앞서는 순열은 오름차순으로 이루어진 순열이고, 가장 마지막에 오는 순열은 내림차순으로 이루어진 순열이다.
N = 3인 경우에 사전순으로 순열을 나열하면 다음과 같다.
1, 2, 3
1, 3, 2
2, 1, 3
2, 3, 1
3, 1, 2
3, 2, 1
첫째 줄에 N(1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄에 순열이 주어진다.
첫째 줄에 입력으로 주어진 순열의 다음에 오는 순열을 출력한다. 만약, 사전순으로 마지막에 오는 순열인 경우에는 -1을 출력한다.
이전에 썼던 백트래킹 방식으로 모든 순열을 저장하고 여기서 다음 순열 찾아내기
var fs = require('fs');
const [N, current] = fs
.readFileSync('/dev/stdin')
.toString()
.trim()
.split('\n');
function solution() {
const NArray = Array(+N)
.fill(0)
.map((_, i) => i + 1);
const permutations = [];
for (let i = 0; i < NArray.length; i++) {
BT(NArray[i], [NArray[i]]);
}
function BT(index, numbers) {
if (numbers.length === +N) return permutations.push(numbers.join(' '));
for (let j = 0; j < NArray.length; j++) {
const number = NArray[j];
if (numbers.includes(number)) continue;
BT(index + 1, [...numbers, number]);
}
}
const index = permutations.findIndex((item) => item === current);
if (index === permutations.length - 1) return console.log(-1);
console.log(permutations[index + 1]);
}
solution();
N(1 ≤ N ≤ 10,000)
으로 모든 순열을 찾게되면 시간초과가 뜬다...
규칙
1) 뒤에서 부터 오름차순이 멈추는 값의 index를 찾는다 : I
2) I이후 값들중 I보다 큰 값중 가장 작은 값의 index을 찾는다 : J
3) I의 값과 J의 값을 바꾼다
4) I이후의 값들을 오름차순으로 정렬해준다.
var fs = require('fs');
const [N, current] = fs
.readFileSync('./input.txt')
.toString()
.trim()
.split('\n');
function solution() {
const permutations = current.split(' ');
let I = -1;
//1) 뒤에서 부터 오름차순이 멈추는 i 값 찾기 : I에 할당
for (let i = permutations.length - 1; i > 0; i--) {
if (+permutations[i] > +permutations[i - 1]) {
I = i - 1;
break;
}
}
// 2) 만약 I가 -1이라면 마지막 순열이므로 -1 출력
if (I === -1) return console.log(-1);
let J = I + 1;
//3) I보다 뒤에 있는 값중 permutations[I]값보다 큰것중 가장 작은 값의 index 구하기 : J에 할당
let min = +permutations[J];
for (let j = J + 1; j < permutations.length; j++) {
if (+permutations[I] < +permutations[j] && min >= +permutations[j]) {
min = +permutations[j];
J = j;
}
}
[permutations[I], permutations[J]] = [permutations[J], permutations[I]];
const left = permutations.slice(0, I + 1);
const right = permutations.slice(I + 1).sort((a, b) => a - b);
const answer = [...left, ...right];
console.log(answer.join(' '));
}
solution();