신고 결과 받기
프로그래머스 신고 결과 받기 문제 풀이
문제설명
신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다.
- 각 유저는 한 번에 한 명의 유저를 신고할 수 있습니다.
- 신고 횟수에 제한은 없습니다. 서로 다른 유저를 계속해서 신고할 수 있습니다.
- 한 유저를 여러 번 신고할 수도 있지만, 동일한 유저에 대한 신고 횟수는 1회로 처리됩니다.
- k번 이상 신고된 유저는 게시판 이용이 정지되며, 해당 유저를 신고한 모든 유저에게 정지 사실을 메일로 발송합니다.
- 유저가 신고한 모든 내용을 취합하여 마지막에 한꺼번에 게시판 이용 정지를 시키면서 정지 메일을 발송합니다.
id_list | report | k | result |
---|---|---|---|
[“muzi”, “frodo”, “apeach”, “neo”] | [“muzi frodo”,”apeach frodo”,”frodo neo”,”muzi neo”,”apeach muzi”] | 2 | [2,1,1,0] |
제한사항
- 2 ≤
id_list
의 길이 ≤ 1,000 - 1 ≤
report
의 길이 ≤ 200,000
O(report * id_list)여도 시간제한을 만족한다.
report를 순회하면서, 신고받은 대상자를 기준으로 자신을 신고한 아이디를 SET 형식(중복없이) 추가한다. 이후 id_list를 순회하면서 자신이 신고기준보다 높으면, 자신을 신고한 아이디 unordered_map에 1을 더해준다. 결국 O(report)로 구현할 수 있다.
소스코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
unordered_map<string, set<string>> report_map;
unordered_map<string, int> idxmap;
vector<string> splitString(const string& str) {
istringstream iss(str);
vector<string> tokens;
string token;
while (iss >> token) {
tokens.push_back(token);
}
return tokens;
}
vector<int> solution(vector<string> id_list, vector<string> report, int k) {
for(const string& i : id_list){
idxmap[i] = 0;
}
for(const string& re : report){
vector<string> vec = splitString(re);
auto it = report_map.find(vec[1]);
if(it == report_map.end()){
set<string> s;
s.insert(vec[0]);
report_map.insert({vec[1], s});
}
else{
it->second.insert(vec[0]);
}
}
for(const auto& i : report_map){
if(i.second.size() >= k){
for(const string& s : i.second){
idxmap[s]++;
}
}
}
vector<int> ans;
for(const string& i : id_list){
ans.push_back(idxmap[i]);
}
return ans;
}
배운점
This post is licensed under CC BY 4.0 by the author.