반응형
https://school.programmers.co.kr/learn/courses/30/lessons/131127
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
또 문제 잘못 읽고 초반에 많이 고쳤지만 오랜만에 한방에 통과했다 조금 나아진 것 같다.
10일간의 세일 품목을 map에 다 넣어주고 그 10일간의 세일 품목이 필요로 하는 품목의 개수보다 많거나 같으면 answer++ 해주고
1일 전 값을 map에서 빼주고, 11일째 값을 넣어주는 식으로 모든 세일 기간을 탐색한다.
좀 잘 푼 거 같다ㅎ.
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
bool checkAllProductSale(unordered_map<string, int> map, vector<string> want, vector<int> number)
{
for (int i = 0; i < want.size(); i++)
if (map.find(want[i]) == map.end() || map[want[i]] < number[i]) // map의 상품 개수가 필요 개수보다 같거나 많으면
return false;
return true;
}
int solution(vector<string> want, vector<int> number, vector<string> discount) {
int answer = 0;
unordered_map<string, int> map;
for (int i = 0; i < 10; i++) // 1 ~ 10일간의 할인 품목을 map에 다 넣어줌
{
if (map.find(discount[i]) == map.end())
map[discount[i]] = 1;
else
map[discount[i]] += 1;
}
for (int i = 10; i <= discount.size(); i++)
{
if (checkAllProductSale(map, want, number))
answer++;
if (i == discount.size()) // 마지막이면
break ;
map[discount[i - 10]] -= 1; // i - 10일 꺼 삭제
if (map.find(discount[i]) == map.end()) // i일 꺼 추가
map[discount[i]] = 1;
else
map[discount[i]] += 1;
}
return answer;
}
반응형
'<algorithm> > 프로그래머스' 카테고리의 다른 글
프로그래머스 뉴스 클러스터링 c++ (0) | 2023.07.02 |
---|---|
프로그래머스 정수 삼각형 c++ (2) | 2023.06.28 |
프로그래머스 튜플 c++ (2) | 2023.06.22 |
프로그래머스 행렬의 곱셈 c++ (0) | 2023.06.06 |
프로그래머스 캐시 c++ (0) | 2023.06.05 |