프로그래머스/2레벨

할인 행사/C++

Koalitsiya 2023. 1. 17. 16:08
 

프로그래머스

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

programmers.co.kr

unordered_map의 == 연산자가 map에 저장된 요소의 순서와 상관없이 비교하므로 unordered_map 2개를 선언하여 두 개가 같으면 가능한 날이므로 answer++하여 제품을 모두 할인 받을 수 있는 회원등록 날짜의 총 일수를 구하였다.

 

 

1. string을 Key, int를 value로 가지는 unordered_map 2개를 선언한다.

2. wants라는 unordered_map에는 want[i], number[i]를 페어로 하여 삽입한다.

3. discounts에는 i부터 i + 10까지 discounts[discount[j]]++을 하고 wants == discounts가 true면 count++을 한다.

4. discounts를 비워주고 i++을 한 다음 위를 반복

5. 반복문이 끝나면 count를 리턴

#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

int solution(vector<string> want, vector<int> number, vector<string> discount) {
    int answer = 0;
    int count = 0;
    
    unordered_map<string, int> wants;
    unordered_map<string, int> discounts;
    
    for(int i = 0; i < want.size(); i++)
        wants.insert({want[i], number[i]});
    
    for(int i = 0; i <= discount.size() - 10; i++) {
        
        for(int j = i; j < i + 10; j++)
            discounts[discount[j]]++;
        
        if(wants == discounts) count++;
        discounts.clear();
    }
    
    answer = count;
    
    return answer;
}