programmers_이모티콘 할인행사_java

2023. 5. 25. 16:02Algorithm/Programmers

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/150368

class Solution {
    
    private static int[] res;
    public int[] solution(int[][] users, int[] emoticons) {
        int[] answer = {};
        double[] salePercentInfo = {10, 20, 30, 40};
        res = new int[]{0,0};
        dfs(emoticons, users, salePercentInfo, new int[users.length],  0, new double[emoticons.length]);

        answer = res;
        return answer;
    }

    public void dfs(int[] emoticons, int[][] users, double[] salePercentInfo, int[] userTotalPrice, int cnt, double[] sale){
        if (cnt == emoticons.length){
            int[] promotionRes = promotion(users, userTotalPrice);
            if (promotionRes[0] > res[0]){
                res = promotionRes;
            }else if(promotionRes[0] == res[0]){
                res[1] = Math.max(promotionRes[1], res[1]);
            }

            return;
        }

        for (int i = 0; i< salePercentInfo.length; i++){
            // 해당 이모티콘 구매
            for (int j = 0; j< users.length; j++){
                if (users[j][0] <= salePercentInfo[i]){
                    userTotalPrice[j] += emoticons[cnt] * (100-salePercentInfo[i]) /100;
                }
            }
            sale[cnt] = salePercentInfo[i];

            dfs(emoticons, users, salePercentInfo, userTotalPrice, cnt+1, sale);
            for (int j = 0; j< users.length; j++){
                if (users[j][0] <= salePercentInfo[i]){ // 할인 여부
                    userTotalPrice[j] -= emoticons[cnt] * (100-salePercentInfo[i]) /100;
                }
            }
        }
    }

    public int[] promotion(int[][] users, int[] userTotalPrice){
        int priceSum = 0;
        int emoticonPlus = 0;
        for (int i = 0; i<users.length; i++){
            if (userTotalPrice[i] >= users[i][1]){
                emoticonPlus++;
            }else {
                priceSum += userTotalPrice[i];
            }
        }
        return new int[]{emoticonPlus, priceSum};
    }

}

'Algorithm > Programmers' 카테고리의 다른 글

programmers_과제 진행하기_java  (1) 2023.05.26
programmers_요격 시스템_java  (0) 2023.05.25
programmers_순위 검색_java  (0) 2023.05.25
programmers_혼자서 하는 틱택토_java  (0) 2023.05.24
programmers_숫자 블록_java  (0) 2023.05.22