프로그래머스 - 신고 결과 받기

 

프로그래머스

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

programmers.co.kr

풀이 방법

1. answer를 id_list.size()만큼 0으로 초기화 시켜준다.

2. map 자료형 id_lists를 선언하고 id_list와 각 원소의 인덱스를 각각 key,value로 저장한다.

3. 문자열을 공백을 기준으로 두 개의 문자열 from, to로 자르고 reports2[to]에 from을 insert한다.

4. reports를 순회하며 각 key 값의 value의 길이가 k이상이면 해당 value에 들어있는 id에 ++해준다.

5. answer 리턴

#include <string>
#include <vector>
#include <map>
#include <set>

using namespace std;

vector<int> solution(vector<string> id_list, vector<string> reports, int k) {
    vector<int> answer(id_list.size(),0);
    
    map<string, int> id_lists;
    map<string, set<string>> reports2;
    
    for (int i=0; i < id_list.size(); ++i){
        id_lists[id_list[i]] = i;
    }

    
    for(auto &report : reports){
        string from = report.substr(0, report.find(' '));
        string to = report.substr(report.find(' ') + 1, report.length() - report.find(' '));
        
        reports2[to].insert(from);
    }
    
    for (auto rep : reports2){
        if (rep.second.size() >= k){
            for (auto list : rep.second){
                answer[id_lists[list]]++;
            }
        }
    }
    
    return answer;
}

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

개인정보 수집 유효기간/C++  (0) 2023.02.06
둘만의 암호/C++  (0) 2023.02.03
성격 유형 검사하기/C++  (0) 2023.01.03
숫자 짝꿍/C++  (1) 2023.01.03
푸드 파이트 대회/C++  (0) 2023.01.03

+ Recent posts