[프로그래머스] 이모티콘 할인 행사

문제

프로그램 제작자

코드 중심 개발자를 고용하십시오. 배치 기반 위치 매칭. 프로그래머의 개발자별 프로필에 가입하고 기술 호환성이 좋은 회사와 연결하십시오.

Programmer.co.kr

해결

이 문제는 전체 검색으로 해결할 수 있습니다. 할인 비율은 10%, 20%, 30% 또는 40%로 설정됩니다. 각 이모티콘에 대한 할인율을 결정하고 이에 따라 Emoji Plus 구독자 수와 총 판매량을 계산할 수 있습니다. 따라서 가능한 모든 할인율을 찾아 이 둘 중 가장 큰 값을 찾아야 합니다.

DFS는 역추적을 통해 가능한 할인율의 순열을 반환합니다.

private static final int() RATE = {90, 80, 70, 60};

private void dfs(int() emoticons, int()() users, int cur, int() rates) {
    if (cur == emoticons.length) {
        updateAnswer(emoticons, users, rates);
        return;
    }

    for (int rate : RATE) {
        rates(cur) = rate;
        dfs(emoticons, users, cur + 1, rates);
    }
}

할인율 순열을 취하여 각 사용자가 Emoji Plus에 가입했는지 여부에 따라 수익이 얼마인지 계산합니다. 계산된 결과로 정답 배열을 업데이트합니다.

private static int EMOTICON_PLUS = 0;
private static int TOTAL_SALES = 0;

private void updateAnswer(int() emoticons, int()() users, int() rates) {

    int ePlus = 0;
    int totalExpense = 0;

    for (int() user : users) {
        int expense = 0;
        int rate = user(0);
        int price = user(1);
        for (int i = 0; i < rates.length; i++) {
            if (100 - rates(i) >= rate) {
                expense += emoticons(i) * rates(i) / 100;
            }
            if (expense >= price) {
                ePlus += 1;
                expense = 0;
                break;
            }
        }
        totalExpense += expense;
    }

    if (ePlus > EMOTICON_PLUS) {
        EMOTICON_PLUS = ePlus;
        TOTAL_SALES = totalExpense;
    } else if (ePlus == EMOTICON_PLUS) {
        TOTAL_SALES = Math.max(totalExpense, TOTAL_SALES);
    }
}

코드

import java.util.Arrays;

class Solution {

    private static final int() RATE = {90, 80, 70, 60};
    private static int EMOTICON_PLUS = 0;
    private static int TOTAL_SALES = 0;

    public int() solution(int()() users, int() emoticons) {
        getPrices(emoticons, users, 0, new int(emoticons.length));
        return new int(){EMOTICON_PLUS, TOTAL_SALES};
    }

    private void getPrices(int() emoticons, int()() users, int cur, int() rates) {
        if (cur == emoticons.length) {
            updateAnswer(emoticons, users, rates);
            return;
        }

        for (int rate : RATE) {
            rates(cur) = rate;
            getPrices(emoticons, users, cur + 1, rates);
        }
    }

    private void updateAnswer(int() emoticons, int()() users, int() rates) {

        int ePlus = 0;
        int totalExpense = 0;

        for (int() user : users) {
            int expense = 0;
            int rate = user(0);
            int price = user(1);
            for (int i = 0; i < rates.length; i++) {
                if (100 - rates(i) >= rate) {
                    expense += emoticons(i) * rates(i) / 100;
                }
                if (expense >= price) {
                    ePlus += 1;
                    expense = 0;
                    break;
                }
            }
            totalExpense += expense;
        }

        if (ePlus > EMOTICON_PLUS) {
            EMOTICON_PLUS = ePlus;
            TOTAL_SALES = totalExpense;
        } else if (ePlus == EMOTICON_PLUS) {
            TOTAL_SALES = Math.max(totalExpense, TOTAL_SALES);
        }
    }


}