2023. 7. 9. 18:54γCoding Test/Programmers
πλ¬Έμ 보λ¬κ°κΈ°
π¨π»νμ΄ κ³Όμ
μμ μ¬λμ μ μ μ μκ³ , λ°₯ λ¨Ήκ³ , μ»€νΌ ν μ λ§μλ©΄μ μ§νν΄μΌ λ₯λ₯ μ΄ μ’λ λ΄ λλ€. μ λ²μ ν λ λ¨Έλ¦¬κ° μ λμκ°μ κ·Έλ₯ μκ°λ§ 보λ΄κ³ μμλλ°, μ΄λ²μ μΉ΄νμ μμ μ μ 컨λμ μΌλ‘ νΈλκΉ κΈλ°© νμλ€μ.
ν맀νλ μ΄λͺ¨ν°μ½μ μ΅λ κ°μλ 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;
}
'Coding Test > Programmers' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Programmers] Lv2. μΉ΄μΉ΄μ€νλ μ¦ μ»¬λ¬λ§λΆ | C++ (0) | 2023.07.13 |
---|---|
[Programmers] Lv2. μμ κ²μ | C++ (0) | 2023.07.10 |
[Programmers] Lv2. μ격 μμ€ν | C++ (0) | 2023.07.06 |
[Programmers] Lv2. νΌμμ νλ ν±νν | C++ (0) | 2023.07.05 |
[Programmers] Lv2. μ«μ λΈλ‘ | C++ (0) | 2023.07.04 |