08
30

https://school.programmers.co.kr/learn/challenges?tab=algorithm_practice_kit

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

ํ‘ธ๋Š” ๋™์•ˆ ์‚ฌ๊ณ ๊ณผ์ • ๋ฐ ๊ฐœ์„ ์ 

import java.util.*;

class Solution {
    static boolean[] v; 
    static String[] out;
    static int n;
    static TreeSet<String> pq = new TreeSet<String>(Collections.reverseOrder());
    
    public static void perm(int[] num, int depth){
        if(depth == n){
            StringBuilder sb = new StringBuilder();
            for(String item: out){
                sb.append(item);
            }
            pq.add(sb.toString());
            return;
        }
        for(int i=0; i<num.length; i++){
            if(!v[i]){
                //๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด ๋‹ค์Œ์œ„์น˜์— ๋„ฃ๊ธฐ
                v[i] = true;
                out[depth] = Integer.toString(num[i]);
                perm(num, depth+1);
                v[i] = false;
            }
        }
    }
    public String solution(int[] numbers) {
        String answer = "";
        // ์ˆœ์—ด๋กœ ์ผ๋‹จ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ, PriorityQueue์— ๋„ฃ๊ธฐ
        n = numbers.length;
        v = new boolean[n];
        out = new String[n];
        perm(numbers, 0);
        
        answer = String.valueOf(pq.first());
        return answer;
    }
}

์ฒ˜์Œ์—๋Š” ์ˆœ์—ด๋กœ ์ ‘๊ทผํ•˜๋ฉด ๋  ์ค„ ์•Œ์•˜๋‹ค. TreeSet์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์—๋Š” ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์จ์„œ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ทผ๋ฐ input์„ ๋ณด๋ฉด

numbers ๊ธธ์ด๊ฐ€ 100000์ธ๋ฐ, ์ด๋Ÿฌ๋ฉด ์ˆœ์—ด์ƒ์„ฑ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ n!๋ผ์„œ ๋ฌด์กฐ๊ฑด ์‹œ๊ฐ„์ดˆ๊ณผ, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

๋ฐฉ๋ฒ•์„ ๋ฐ”๊ฟ”์•ผ๋œ๋‹ค. 

import java.util.*;

class Solution {
    public String solution(int[] numbers) {
        String[] strNumbers = new String[numbers.length];
        
        for(int i = 0; i < numbers.length; i++) {
            strNumbers[i] = String.valueOf(numbers[i]);
        }
        
        Arrays.sort(strNumbers, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return (b + a).compareTo(a + b);
            }
        });
        
        if(strNumbers[0].equals("0")) return "0";
        
        StringBuilder answer = new StringBuilder();
        for(String s : strNumbers) {
            answer.append(s);
        }
        
        return answer.toString();
    }
}

String์œผ๋กœ ๋‹ค ๋ฐ”๊พผ๋‹ค์Œ, Comparator๋ฅผ ์ƒˆ๋กœ ์ •์˜ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ๋ช‡๋ฒˆ ํ’€์—ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋Š” ๊ฑด ์ฒ˜์Œ์ด๋‹ค...


# ์ถ”๊ฐ€ ๊ณต๋ถ€

`Arrays.sort(arr, new Comparator<T>(){ })`

Arrays.sort(strNumbers, new Comparator<String>() {
    @Override
    public int compare(String a, String b) {
        return (b + a).compareTo(a + b);
    }
});

์˜ค๋žœ๋งŒ์— Comparator๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ๊นŒ ์ง„์งœ ์ƒ๊ฐ์ด ์•ˆ๋‚ฌ๋‹ค.

Arrays๋Š” ๋ฐฐ์—ด์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ, ์•ˆ์“ฐ๋ฉด ๊ณ„์† ๊นŒ๋จน์–ด์„œ ์ผ๋‹จ ์ ์–ด๋‘”๋‹ค. ๊ฐ™์„ ๋•Œ(๊ธฐ์กด ์ˆœ์„œ ์œ ์ง€), ์ž‘์„ ๋•Œ(-1), ํด ๋•Œ(1)๋ฅผ ๊ฐ๊ฐ ๊ธฐ์ค€์œผ๋กœ ๋‘๊ณ  ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.

 

 

 

๋„์›€์ด ๋๋‹ค๋ฉด ๋Œ“๊ธ€์ด๋‚˜ ๊ณต๊ฐ ๋ฒ„ํŠผ ํ•œ ๋ฒˆ์”ฉ ๋ˆ„๋ฅด๊ณ  ๊ฐ€์ฃผ์„ธ์š”!

 

๋ฐ˜์‘ํ˜•
COMMENT