[Programmers] Lv2. 이λͺ¨ν‹°μ½˜ 할인행사 | C++

2023. 7. 9. 18:54ㆍCoding Test/Programmers

πŸ”—λ¬Έμ œ λ³΄λŸ¬κ°€κΈ°
 

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€

μ½”λ“œ μ€‘μ‹¬μ˜ 개발자 μ±„μš©. μŠ€νƒ 기반의 ν¬μ§€μ…˜ 맀칭. ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€μ˜ 개발자 λ§žμΆ€ν˜• ν”„λ‘œν•„μ„ λ“±λ‘ν•˜κ³ , λ‚˜μ™€ 기술 ꢁ합이 잘 λ§žλŠ” 기업듀을 맀칭 λ°›μœΌμ„Έμš”.

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
λ°˜μ‘ν˜•