728x90
🔗 문제 링크
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
💡 문제 풀이 및 해석
- 사용자마다
map<userId, set<userId>>
를 사용해서 신고한 횟수가 아닌 신고 당한 ID를 모은다. - 신고 당한 ID의 갯수 = 나를 신고한 사람이므로, 이 점을 이용해서 풀면 된다.
⭐️ 정답 코드 및 설명
#include <string>
#include <vector>
#include <set>
#include <map>
#include <iostream>
using namespace std;
map<string, set<string>> myReport; // 유저 : 해당 유저를 신고한 ID
map<string,int> user; // id : index
vector<int> solution(vector<string> id_list, vector<string> report, int k) {
vector<int> answer;
answer=vector<int>(id_list.size(), 0);
int index=0;
for(string id : id_list){
user[id]=index++; // id : index
}
for(string str : report){
string userId="",reportedId="";
bool flag=false;
for(char c : str){
if(c==' '){
flag=true;
continue;
}
if(flag){
reportedId+=c;
continue;
}
userId+=c;
}
myReport[reportedId].insert(userId);
}
for(string id : id_list){
if(myReport[id].size()>=k){
for(string reportMe : myReport[id]){
int i = user[reportMe];
answer[i]++;
}
}
}
return answer;
}
🤔 문제 후기
map과 set을 이용하면 풀기에는 어렵지 않은 문제였지만, 그 중간에 Key : Value
에 대해서 실수만 하지 않으면 풀기에 어려운 문제는 아니였다. sstream
을 사용하면, 쉽게 하는 방법이 있었는데, 정확하게 기억이 나지 않아서 flag
를 사용해서 문자열을 잘라냈다.
728x90
'문제풀이 > 알고리즘 문제 풀이' 카테고리의 다른 글
[ 프로그래머스 ] 주차 요금 계산 (C++) - 구현 (0) | 2024.04.05 |
---|---|
[ 프로그래머스 ] K진수에서 소수 구하기 (C++) - 수학 (0) | 2024.04.05 |
[ 백준 1759 ] 암호 만들기 (C++) - 조합, BackTracking (0) | 2024.04.04 |
[ 백준 16120 ] PPAP (C++) - 그리디 (0) | 2024.04.02 |
[ 백준 12919 ] A와 B 2 (C++) - DFS (0) | 2024.04.01 |