[프로그래머스] 신고결과 받기 (C++)
in CodingTest
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include<set>
using namespace std;
map<string,int> reportCount;
map<string,set<string>> reportLog;
vector<int> solution(vector<string> id_list, vector<string> report, int k) {
vector<int> answer;
for(string s : report){
int blank = s.find(' ');
string from = s.substr(0,blank);
string to = s.substr(blank);
if(reportLog[from].find(to) == reportLog[from].end()){
reportCount[to]++;
reportLog[from].insert(to);
}
}
for(string s : id_list)
{
int result=0;
for(string str : reportLog[s])
{
if(reportCount[str]>=k)
{
result++;
}
}
answer.push_back(result);
}
return answer;
}
이번 문제에서 중요했던 점은 어떤 자료구조를 선택하여 문제를 해결할 것이냐를 보는 것 같다. 이 문제를 해결하기 위한 핵심 자료구조는 아래와 같다.
- 유저 당 신고당한 횟수를 저장할 자료구조 (map<string,int>)
- 유저 당 신고한 유저의 리스트를 저장할 자료구조 (map<string,set<string>)
유저 당 신고당한 횟수를 저장할 자료구조는 유저들의 고유한 문자열 형태의 id값을 가지고 있고 신고당한 횟수는 정수 형태로 저장될 것이기 때문에 자연스럽게
map<string,int>자료구조가 떠오른다.
유저 당 신고한 유저의 리스트를 저장할 자료구조는 유저들의 고유한 문자열 형태의 id값을 가지고 있고 각 id마다 신고한 유저들이 있기에 여러 문자열을 저장할 자료구조를 떠올려야하는데 문제의 조건 중 동일한 유저에 대한 신고는 중복없이 1회로 처리한다라는 조건 때문에 (map<string,set<string>)을 선택하게 되었다.
