프로그래머스

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

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;
}

'프로그래머스 > 2레벨' 카테고리의 다른 글

k진수에서 소수 개수 구하기/C++  (0) 2023.01.18
124 나라의 숫자/C++  (0) 2023.01.17
연속 부분 수열 합의 개수/C++  (0) 2023.01.17
n^2 배열 자르기/C++  (0) 2023.01.17
가장 큰 수/C++  (0) 2023.01.17

+ Recent posts