ALGORITHM/PROGRAMMERS

[프로그래머스] 메뉴 리뉴얼 (2021 KAKAO BLIND RECRUITMENT)

0298 2021. 2. 4. 20:54

programmers.co.kr/learn/courses/30/lessons/72411

 

코딩테스트 연습 - 메뉴 리뉴얼

레스토랑을 운영하던 스카피는 코로나19로 인한 불경기를 극복하고자 메뉴를 새로 구성하려고 고민하고 있습니다. 기존에는 단품으로만 제공하던 메뉴를 조합해서 코스요리 형태로 재구성해서

programmers.co.kr

2021-02-04


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;
public class Solution72411 {
    public static HashMap<String, Integer> map;
    public static ArrayList<String> ans;
    public static int arr[];
    public static boolean vtd[];
    public static void solve(String orders, int course, int count, int idx) {
         if(count == course) {
             String temp[] = new String[course];
             for(int i = 0; i < count; i++) {
                 temp[i] = String.valueOf(orders.charAt(arr[i]));
             } 
             Arrays.sort(temp); // 만들 수 있는 조합 번호로 음식을 만들 때, 오름차순으로 정렬
             String str = "";
             for(int i = 0; i < temp.length; i++) str += temp[i]; 
             if(map.containsKey(str)) { // 만약 나온적 있던 음식의 조합이라면 +1 추가 후 put 
                 int val = map.get(str);
                 map.put(str, val+1);
             } else map.put(str, 1); // 한 번도 나온 적 없던 음식의 조합이라면 1로 put
             return;
         }
 
         for(int i = idx; i < orders.length(); i++) { // 조합
             if(!vtd[i]) {
                 vtd[i] = true;
                 arr[count] = i;
                 solve(orders, course, count + 1, i);
                 vtd[i] = false;
             }
         }
    }
    public static String[] solution(String[] orders, int[] course) {
        ans = new ArrayList<>();
        int maxLength = 0;
        for(int i = 0; i < orders.length; i++) maxLength = Math.max(maxLength, orders[i].length()); // orders 가장 긴 값
        for(int i = 0; i < course.length; i++) { 
            if(course[i] <= maxLength) { 
                arr = new int[course[i]]; // course의 길이와 같은 arr 배열 생성 - 조합으로 넣을 예정
                map = new HashMap<>(); // string(조합된 음식)을 key로, Integer(갯수)를 value로
                for(int j = 0; j < orders.length; j++) { // order만큼 돌면서 만들 수 있는 조합 
                    vtd = new boolean[orders[j].length()];
                    solve(orders[j], course[i], 00);
                }
                int findMax = Collections.max(map.values()); // value값 중 가장 큰 값을 찾는다.
                if(findMax >= 2) { // value값이 2 이상인 것 중에서 
                    for(Map.Entry<String, Integer> m: map.entrySet()) { // map을 돌면서 
                        if(m.getValue() == findMax) { // findMax value를 갖고 있는 key들을 정답 list에 넣는다.(1개 이상일 수 있으니 돌면서 다 체크해본다)
                            ans.add(m.getKey());
                        }
                    }
                }
            }
        }
        Collections.sort(ans);
        String answer[] = new String[ans.size()];
        for(int i = 0; i < ans.size(); i++) answer[i] = ans.get(i);
        return answer;
    }
    
    public static void main(String[] args) {
        String o[] = {"XYZ""XWY""WXA"};
        int c[] = {234};
        
        System.out.println(Arrays.toString(solution(o, c)));
    }
}
 
cs

 

#문제풀이

 

좀 지저분하게 풀었다라는 느낌이 좀 들긴하다.

 

내가 푼 코드 상에서는 조합과 HashMap이 포인트 인 것 같다.

 

자세한 내용은 주석으로 달아놨다.