# 생각
map을 통해 문자열을 숫자 인덱스로 쉽게 접근하고
set을 통해 같은 유저에게 신고는 1회 처리를 쉽게 관리할 수 있다
# 전체 코드
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<string> id_list, vector<string> report, int k) {
map<string, int> stoi; // id to int 인덱스
int n = id_list.size();
set<int> s[1000]; // 유일 원소 집합을 사용하기 위한 set 컨테이너
vector<int> count(n);
for(int i = 0; i < n; i++)
{
stoi[id_list[i]] = i;
}
for(auto& rep : report)
{
stringstream st(rep); // 공백처리
string id1, id2;
st >> id1 >> id2;
s[stoi[id2]].insert(stoi[id1]);
}
for(int i = 0; i < n; i++)
{
if(s[i].size() < k) continue;
for(auto x : s[i]) count[x]++;
}
return count;
}
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
'Algorithm > 프로그래머스' 카테고리의 다른 글
[Lv.1] 완주하지 못한 선수 with cpp (0) | 2022.02.28 |
---|---|
[Lv.1] 크레인 인형뽑기 게임 with cpp (0) | 2022.02.20 |
[Lv.1] 키패드 누르기 with cpp (0) | 2022.02.20 |
[Lv.1] 신규 아이디 추천 with cpp (0) | 2022.02.10 |
[Lv.1] 로또의 최고 순위와 최저 순위 with cpp (0) | 2022.02.09 |