링크: https://programmers.co.kr/learn/courses/30/lessons/92334
특정 유저를 신고했을 때, 그 유저를 신고한 유저들이 받게되는 이메일의 횟수를 구하는 문제
코드:
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 48 49 50 51 52 53 54 | #include <vector> #include <string> #include <algorithm> #include <map> using namespace std; vector<int> solution(vector<string> id_list, vector<string> report, int k) { vector<int> answer; /* ["muzi", "frodo", "apeach", "neo"], ["muzi frodo", "apeach frodo", "frodo neo", "muzi neo", "apeach muzi"] */ map<string, int> reportCountMap; map<string, vector<string>> reportUserMap; // key는 신고 당한 유저, value는 이 유저를 신고한 유저들 for (auto i : id_list) { vector<string> t; reportUserMap.insert({i, t}); reportCountMap.insert({i, 0}); } for (string item : report) { int idx = item.find(" ", 0); string id = item.substr(0, idx); // muzi (muzi -> frodo) string id2 = item.substr(idx + 1); // frodo // 중복 제거 if (find(reportUserMap[id2].begin(), reportUserMap[id2].end(), id) == reportUserMap[id2].end()) { reportUserMap[id2].push_back(id); } } for (auto i : reportUserMap) { // 유저의 신고 당한 횟수가 k 회 이상이면, (i.second는 i.first 유저를 신고한 유저 리스트) if (i.second.size() >= k) { for (auto a : i.second) { reportCountMap[a]++; } } } for (auto id : id_list) { // id_list 의 순서대로, id에 해당하는 유저가 받게 되는 메일의 수 answer.push_back(reportCountMap[id]); } return answer; } |
댓글
댓글 쓰기