본문 바로가기

책/프로그래머스

[ 프로그래머스] #131127 할인행사

https://school.programmers.co.kr/learn/courses/30/lessons/131127

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

#include <string>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <utility>

using namespace std;

void ResetMap(map<string, int>& wantmap, queue<string>& List)
{
	while (List.size() > 0)
	{
		string& s = List.front();
		if (wantmap.find(s) == wantmap.end())
		{
			wantmap.emplace(s, 1);
		}
		else
		{
			wantmap[s] += 1;
		}
		List.pop();
	};
}

bool checkBuy(vector<string>& want, map<string, int>& wantmap)
{
	for (string& w : want)
	{
		if (wantmap[w] > 0)
		{
			return false;
		}
	}
	return true;
}

int solution(vector<string> want, vector<int> number, vector<string> discount) {
	int answer = 0;
	queue<string> List;
	queue<int> Listpushday;
	map<string, int> wantmap;
	for (int i = 0; i < want.size(); ++i)
	{
		wantmap.emplace(want[i], number[i]);
	}

	int tempday = 0;
	for (string& dis : discount)
	{
		tempday += 1;
		//찾은 경우
		//그날 할인 체크
		if (wantmap.find(dis) != wantmap.end())
		{
			List.push(dis);
			Listpushday.push(tempday);
			wantmap[dis] -= 1;
		}
		//10일이 넘은 물건 체크 - List는 일치한 날일 뿐임.
		if (tempday > 10 && Listpushday.front() == tempday -10)
		{
			string& popproduct = List.front();
			wantmap[popproduct] += 1;
			List.pop();
			Listpushday.pop();
		}

		//가입 가능한지 체크
		if (checkBuy(want, wantmap) == true)
		{
			answer += 1;
		}
		
	}

	return answer;
}

int main()
{
	int answer = solution({ "banana", "apple", "rice", "pork", "pot" }, { 3, 2, 2, 2, 1 }, { "chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana" });
	return answer;
}