기본 콘텐츠로 건너뛰기

프로그래머스 - 줄서는 방법

문제가 점점 어려워지는군욤
import java.util.ArrayList;
import java.util.Arrays;

public class LineCombination {
    public int[] setAlign(int n, long k) {
        int[] answer = new int[n];
        ArrayList<Integer> list = new ArrayList<Integer>();

        int[] fac = new int[n+1];
        fac[0] = 1;
        for(int i=1;i<n;i++) {
            fac[i] = fac[i-1]*i; 
        }

        int idx = 0;
        for(int i=0;i<n;i++) {
            list.add(i+1);
        }

        for(int i=0;i<n;i++) {
            idx=(int) ((k-1)/fac[n-1-i]);
            answer[i]=list.get(idx);
            list.remove(idx);
            k=(k-1)%fac[n-1-i]+1;       
        }       

        return answer;
    }

    // 아래는 테스트로 출력해 보기 위한 코드입니다.
    public static void main(String[] args) {
        LineCombination lc = new LineCombination();
        System.out.println(Arrays.toString(lc.setAlign(4, 1)));
    }
}

댓글

이 블로그의 인기 게시물

프로그래머스 - 시저 암호

public class Caesar { String caesar ( String s , int n ) { n %= 26 ; String result = "" ; char [] temp = s . toCharArray (); for ( int i = 0 ; i < temp . length ; i ++) { if ( temp [ i ]>= 'a' && temp [ i ]<= 'z' ) temp [ i ] = ( char ) (( temp [ i ]+ n )> 'z' ? ( temp [ i ]+ n - 1 )% 'z' + 'a' : ( temp [ i ]+ n )); else if ( temp [ i ]>= 'A' && temp [ i ]<= 'Z' ) temp [ i ] = ( char ) (( temp [ i ]+ n )> 'Z' ? ( temp [ i ]+ n - 1 )% 'Z' + 'A' : ( temp [ i ]+ n )); result += temp [ i ]; } return result ; } public static void main ( String [] args ) { Caesar c = new Caesar (); System . out . println ( "s는 'a B z', n은 4인 경우: " + c . caesar ( ...