[프로그래머스] 할인 행사
풀이
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;
bool isSignUpAppropriate(vector<int> wantNumber, vector<int> discountNumber){
for(int i = 0; i<wantNumber.size(); ++i){
if(wantNumber[i] > discountNumber[i]) return false;
}
return true;
}
int solution(vector<string> want, vector<int> number, vector<string> discount) {
map<string, int> wantIndex = map<string, int>();
vector<int> wantNumber = vector<int>();
vector<int> discountNumber = vector<int>();
for(int i = 0; i<want.size(); ++i){
wantIndex[want[i]] = i;
wantNumber.push_back(number[i]);
discountNumber.push_back(0);
}
int answer = 0;
for(int i = 0; i<discount.size(); ++i){
if(i>=10){
if(wantIndex.find(discount[i-10]) != wantIndex.end()){
int discountIndex = wantIndex[discount[i-10]];
discountNumber[discountIndex] -= 1;
}
}
if(wantIndex.find(discount[i]) != wantIndex.end()){
int discountIndex = wantIndex[discount[i]];
discountNumber[discountIndex] += 1;
}
if(isSignUpAppropriate(wantNumber, discountNumber)){
answer++;
}
}
return answer;
}