10일 동안의 할인 품목들로 원하는 물품을 모두 구매할 수 있는 경우의 수를 구하는 문제.
Map을 이용해 원하는 물품이 등장한 횟수를 관리한다. 원하는 물품이 아닌 경우에는 결과에 영향을 미치지 않으므로 카운트하지 않아도 된다.
https://school.programmers.co.kr/learn/courses/30/lessons/131127
cpp code
#include <map>
#include <vector>
using namespace std;
map<string, int> M;
bool chk(void) {
bool succeed = true;
for (pair<string, int> p : M) {
if (p.second > 0) {
succeed = false;
break;
}
}
return succeed;
}
int solution(vector<string> want, vector<int> number, vector<string> discount) {
int ans = 0;
for (int i=0; i<want.size(); i++) {
M[want[i]] = number[i];
}
for (int i=0; i<10; i++) {
if (M.find(discount[i]) != M.end()) {
M[discount[i]]--;
}
}
ans += chk();
for (int i=10; i<discount.size(); i++) {
if (M.find(discount[i]) != M.end()) {
M[discount[i]]--;
}
if (M.find(discount[i - 10]) != M.end()) {
M[discount[i - 10]]++;
}
ans += chk();
}
return ans;
}