๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

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

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

programmers.co.kr

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

์—ญ์‹œ ์‚ฌ๋žŒ์€ ์ž ์„ ์ž˜ ์ž๊ณ , ๋ฐฅ ๋จน๊ณ , ์ปคํ”ผ ํ•œ ์ž” ๋งˆ์‹œ๋ฉด์„œ ์ง„ํ–‰ํ•ด์•ผ ๋Šฅ๋ฅ ์ด ์ข‹๋‚˜ ๋ด…๋‹ˆ๋‹ค. ์ €๋ฒˆ์— ํ’€ ๋• ๋จธ๋ฆฌ๊ฐ€ ์•ˆ ๋Œ์•„๊ฐ€์„œ ๊ทธ๋ƒฅ ์‹œ๊ฐ„๋งŒ ๋ณด๋‚ด๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, ์ด๋ฒˆ์—” ์นดํŽ˜์— ์™€์„œ ์ •์ƒ ์ปจ๋””์…˜์œผ๋กœ ํ‘ธ๋‹ˆ๊นŒ ๊ธˆ๋ฐฉ ํ’€์—ˆ๋„ค์š”.

 

ํŒ๋งคํ•˜๋Š” ์ด๋ชจํ‹ฐ์ฝ˜์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 7๊ฐœ์ด๊ณ , ํ• ์ธ์€ 40%, 30%, 20%, 10% ์ด๋ ‡๊ฒŒ 4๊ฐ€์ง€ ๋ฟ์ด๋ฏ€๋กœ ์ตœ์•…์ผ ๋•Œ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” \( 4 ^ 7 = 16,384 \) ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ์œ ์ €์˜ ์ตœ๋Œ€ ์ˆ˜๊ฐ€ 100๋ช…์ด๋ฏ€๋กœ \( 100 \times 7 \times 16,384 \) ๋ฒˆ์˜ ์—ฐ์‚ฐ์ด ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค. ์ด๋Š” 1์–ต๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ ์ถฉ๋ถ„ํžˆ ๊ฐ€๋Šฅํ•˜๊ฒ ๋„ค์š”.

 

๊ทธ๋ž˜์„œ ์ €๋Š” ๋ฐฑํŠธ๋ž˜ํ‚น(Backtracking)์œผ๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ฒ€์‚ฌํ•˜์—ฌ, ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ 1. ๊ฐ€์ž…์ž ์ˆ˜ ์ตœ๋Œ€, 2. ํŒ๋งค์•ก ์ตœ๋Œ€์ผ ๋•Œ ๋‹ต์„ ๊ฐฑ์‹ ํ•˜๋Š” ํ˜•ํƒœ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <string>
#include <vector>
using namespace std;
using User = vector<vector<int>>;
vector<int> discountRates = { 40, 30, 20, 10 };

struct Emoticon {
    Emoticon() { }
    Emoticon(int originalPrice, int discountRate) : _discountRate(discountRate) {
        _price = originalPrice * (1.0f - (static_cast<float>(discountRate) * 0.01f));
    }

    int _price;
    int _discountRate;
};

void Search(const User& users, const vector<int>& emoticons, vector<Emoticon>& discountedEmoticons, vector<int>& answers, int emoticonIdx) {
    if (discountedEmoticons.size() == emoticons.size()) {
        int totalBuyingPrice = 0;
        int member = 0;

        for (const auto& user : users) {
            int requireDiscountRate = user[0];
            int membershipStandardPrice = user[1];
            int buying = 0;

            for (const auto& emoticon : discountedEmoticons) {
                if (emoticon._discountRate < requireDiscountRate)
                    continue;

                buying += emoticon._price;

                if (buying >= membershipStandardPrice) {
                    member++;
                    break;
                }
            }

            totalBuyingPrice += (buying < membershipStandardPrice) ? buying : 0;
        }

        if (answers[0] == member and answers[1] < totalBuyingPrice)
            answers[1] = totalBuyingPrice;

        else if (answers[0] < member) {
            answers[0] = member;
            answers[1] = totalBuyingPrice;
        }

        return;
    }

    for (int ei = emoticonIdx; ei < emoticons.size(); ei++) {
        for (int di = 0; di < discountRates.size(); di++) {
            discountedEmoticons.emplace_back(Emoticon(emoticons[ei], discountRates[di]));
            Search(users, emoticons, discountedEmoticons, answers, ei + 1);
            discountedEmoticons.pop_back();
        }
    }
}

vector<int> solution(vector<vector<int>> users, vector<int> emoticons) {
    vector<int> answers { 0, 0 };
    vector<Emoticon> discountedEmoticons;
    
    Search(users, emoticons, discountedEmoticons, answers, 0);
    return answers;
}

 

 

728x90
๋ฐ˜์‘ํ˜•