본문 바로가기
ALGORITHM/C++ - 프로그래머스

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

by DDongYeop 2023. 11. 1.
728x90

문제

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

 

프로그래머스

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

programmers.co.kr

 

 

문제 분석

유저들의 ID가 입력 되고, 그 유저가 어떤 유저를 신고했는지 들어온다. 또 몇 번 신고 받아야 경고 메일이 가는지 들어온다. 

 

유저가 특정 유저 한명당 한명을 신고할 수 있으며 신고를 받은 횟수가 입력 받은 값이 입력 받은 값이 넘었다면 신고 받은 사람에게 메일이 가게 된다. 

 

유저들이 신고한 유저가 메일이 갔는지 안 갔는지 개수를 세어 반환하는 문제이다. 

 

 

알고리즘 설계

유저 ID를 key로 가지고, 신고한 유저 아이디, 신고 받은 횟수, 몇번째 입력 받은 아이디인지 표시해주는 unordered_map을 하나 만들어준다. 

 

for문을 하나 돌려서 id값을 넣어주며 값을 초기화 해준다.  

 

이후 신고 기록을 반복문 돌려 신고 횟수를 늘려주고, 신고 목록에 추가해준다. 

 

다시 반복문을 돌려 입력 받은 신고횟수를 넘겼다면 추가해주고

 

마지막으로 출력해주면 된다. 

 

 

유의점

tuple, map, set에 익숙하지 않다면 어려울 수도 있다. 

 

 

코드

#include <string>
#include <vector>
#include <sstream>
#include <unordered_map>
#include <unordered_set>

using namespace std;

vector<int> solution(vector<string> id_list, vector<string> report, int k) 
{
    int index = 0;
    string str1, str2;
    vector<int> answer(id_list.size());
    unordered_map<string, tuple<unordered_set<string>, int, int>> um;
    unordered_set<string> us;

    for (auto str : id_list)
    {
        um.insert(make_pair(str, make_tuple(unordered_set<string>(), 0, index)));
        ++index;
    }

    for (auto str : report)
    {
        stringstream stream(str);
        stream >> str1 >> str2;

        if (get<0>(um[str1]).find(str2) == get<0>(um[str1]).end())
        {
            get<0>(um[str1]).insert(str2);
            get<1>(um[str2])++;
        }
    }

    for (auto p : um)
    {
        if (get<1>(p.second) >= k)
            us.insert(p.first);
    }

    for (auto p : um)
    {
        for (auto s : us)
        {
            if (get<0>(p.second).find(s) != get<0>(p.second).end())
                answer[get<2>(p.second)]++;
        }
        ++index;
    }

    return answer;
}
728x90

'ALGORITHM > C++ - 프로그래머스' 카테고리의 다른 글

[프로그래머스] 의상  (2) 2023.11.22
[프로그래머스] 입국심사  (1) 2023.11.22
[프로그래머스] 베스트앨범  (0) 2023.11.01

댓글