์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]๋ฉ”๋‰ด ๋ฆฌ๋‰ด์–ผ-java

stonesy 2024. 11. 21. 11:32
728x90

 

๐Ÿ“Œํ’€์ด

1. ๊ฐ ์ฃผ๋ฌธ์„ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ์ •๋ ฌํ•œ ๋’ค, ์กฐํ•ฉ์„ ์ƒ์„ฑํ•œ๋‹ค.

2. ์ƒ์„ฑ๋œ ์กฐํ•ฉ์ด ๋‚˜ํƒ€๋‚˜๋Š” ํšŸ์ˆ˜๋ฅผ ๊ธฐ๋กํ•œ ๋’ค, ๊ฐ ๊ธธ์ด์— ๋Œ€ํ•ด ๊ฐ€์žฅ ๋งŽ์ด ์ฃผ๋ฌธ๋œ ์กฐํ•ฉ์„ ์ฐพ๋Š”๋‹ค.

3. ์ตœ์ข…์ ์œผ๋กœ ๋ชจ๋“  ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ตœ์•…์˜ ๊ฒฝ์šฐ 10*20*2^10

 

๐Ÿ“Œ์ฝ”๋“œ

import java.util.*;

class Solution {
    public static void getCombination(Map<String, Integer> ret, String str, int N, int depth, int start, char[] cur){
        if(depth>=N){
            if(!ret.containsKey(new String(cur))) ret.put(new String(cur), 0);
            ret.put(new String(cur), ret.get(new String(cur))+1);
            return;
        }    
        
        for(int i=start, LEN=str.length(); i<LEN; i++){
            cur[depth]=str.charAt(i);
            getCombination(ret, str, N, depth+1, i+1, cur);
        }
    }
    
    public String[] solution(String[] orders, int[] course) {
        List<String> answer = new ArrayList<>();
        
        for(int c : course) {
            Map<String, Integer> map = new HashMap<>();
            for(String order : orders){
                char[] tmp = order.toCharArray();
                Arrays.sort(tmp);
                String sortedStr = new String(tmp);
                getCombination(map, sortedStr, c, 0, 0, new char[c]);
            }
            
            int maxVal = 2;
            for(int val : map.values()){
                if(val>maxVal) maxVal = val;
            }
            
            for(String key : map.keySet()){
               if(map.get(key)==maxVal) answer.add(key); 
            }
        }
        
        Collections.sort(answer);
        
        return answer.toArray(new String[0]);
    }
}
728x90