곽로그

[백준 10972, Java] 다음 순열 본문

알고리즘/백준

[백준 10972, Java] 다음 순열

일도이동 2021. 4. 25. 21:23
반응형

문제

www.acmicpc.net/problem/10972

 

10972번: 다음 순열

첫째 줄에 입력으로 주어진 순열의 다음에 오는 순열을 출력한다. 만약, 사전순으로 마지막에 오는 순열인 경우에는 -1을 출력한다.

www.acmicpc.net

 

풀이

1 2 3 4 를 순서대로 나열해보자

 

1 2 3 4 

1 2 4 3

1 3 2 4 

  .

  .

  .

4 3 2 1 순으로 나열한다. 이때 1 2 4 3 의 다음 순열인 1 3 2 4 를 보자. 1 2 4 3 의 다음 순열이 왜 1 3 2 4 인지를 보면, 1 2 4 3 이 1 2 - - 으로 시작하는 마지막 순열이기 때문이다. 수를 나열 할때 1 --- 를 나열하고 2---를 나열한다. 1 ---에서도 1 2 --을 나열한다음 1 3 --을 나열한다. 1 2 --로 시작하는 수열의 마지막 수열은 1 2 4 3 이고, 이 때 이 4 3 은 내림 차순이다. 이를 기본아이디어로 다른 수열을 보자

 

1 3 5 4 2의 다음 순열을 찾는 다고 해보자. 1 3 5 4 2 는 1 3 - - -으로 시작하는 수열의 마지막 수열이다. 따라서 다음 수열은 1 4 ---으로 시작해야하고, - - -은 오름차순이 될 것이다. 따라서 1 3 5 4 2의 다음 순열은 1 4 2 3 5이다. 

 

 

 

코드

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st;

        int N = Integer.parseInt(br.readLine());
        int[] numArray = new int[N];

        st = new StringTokenizer(br.readLine()," ");
        for(int i= 0; i<N; i++){
            numArray[i] = Integer.parseInt(st.nextToken());
        }

        int[] results = getNextArray(numArray);
        if(results[0] == -1){
            bw.write("-1");
        }
        else{
            for(int num : results){
                bw.write(String.valueOf(num+" "));
            }

        }
        bw.flush();

    }
    public static int[] getNextArray(int[] numArray){
        //내림차순이 시작되는 인덱스 구하기
        int index = numArray.length-1;
        while(index>0){
            if(numArray[index-1]>numArray[index]){
                --index;
            }
            else{
                break;
            }
        }

        //인덱스에 따른 처리
        if(index ==0){
            numArray[0] = -1;
            return numArray;
        }
        else if(index ==numArray.length-1){
            int temp = numArray[index];
            numArray[index] = numArray[index-1];
            numArray[index-1] = temp;

            return numArray;
        }
        else{
            /*//맨끝에 있는 값(내림차순 수열의 최솟값) 과 index-1의 값 바꾸기
            int temp = numArray[numArray.length-1];
            numArray[numArray.length-1] = numArray[index-1];
            numArray[index-1] = temp;*/

            //index-1의 값과 ,index~N-1 중에서 index-1보다 큰 수 중 가장 작은 수와 바꾸기
            int tempIndex = numArray.length-1;
            while(tempIndex>=index){
                if(numArray[tempIndex]<numArray[index-1]){
                    --tempIndex;
                }
                else{
                    break;
                }
            }
            int temp = numArray[tempIndex];
            numArray[tempIndex] = numArray[index-1];
            numArray[index-1] = temp;


            //index~ N-1까지 오름차순으로 바꾸기
            int[] tempArary = new int[numArray.length-index];
            for(int i = 0; i<tempArary.length;i++){
                tempArary[i] = numArray[i+index];
            }
            Arrays.sort(tempArary);
            for(int i = 0; i<tempArary.length;i++){
                numArray[i+index] = tempArary[i];
            }

            return numArray;
        }

    }
}

 

 

코드 개선

Arrays.sort(정렬할 배열, 시작 인덱스, 끝 인덱스+1, 정렬방법)

단 여기서 배열은 오브젝트형 배열이어야 함

Arrays.sort(array,index, array.length, Collections.reverseOrder());
반응형
Comments