문제
https://school.programmers.co.kr/learn/courses/30/lessons/92334
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
코드
C++
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
map<string, vector<string>> reportingIds;
map<string, int> reportedNums;
vector<int> solution(vector<string> id_list, vector<string> report, int k) {
vector<int> answer;
sort(report.begin(), report.end());
report.erase(unique(report.begin(), report.end()), report.end());
for(int i=0; i<report.size(); i++){
int spaceIdx = report[i].find(' ');
string userId = report[i].substr(0,spaceIdx);
string reportedId = report[i].substr(spaceIdx+1);
reportedNums[reportedId]++;
reportingIds[userId].push_back(reportedId);
}
for (string userId:id_list){
int cnt=0;
for (string reportingId:reportingIds[userId]){
if (reportedNums[reportingId] >=k) cnt++;
}
answer.push_back(cnt);
}
return answer;
}
JS
function solution(id_list, report, k) {
const answer = [];
const set = new Set(report);
report = [...set];
const reportedNums = {}
const reportingIds = {}
for (let id of id_list){
reportedNums[id]=0;
reportingIds[id]=[];
}
for (let i=0; i<report.length; i++){
const [userId, reportedId] = report[i].split(' ');
reportedNums[reportedId]++;
reportingIds[userId].push(reportedId);
}
for (let userId of id_list){
let cnt=0;
if(reportingIds[userId]!==undefined){
for (reportingId of reportingIds[userId]){
if(reportedNums[reportingId] >= k) cnt++;
}
}
answer.push(cnt);
}
return answer;
}
정리
한 유저가 동일한 유저를 여러번 신고해도 신고횟수는 1회로 처리되므로 report 배열의 원소들을 중복 제거한다.
sort(report.begin(), report.end());
report.erase(unique(report.begin(), report.end()), report.end());
userId에 따라 신고당한 횟수, 유저가 신고한 ID를 구한다.
for(int i=0; i<report.size(); i++){
int spaceIdx = report[i].find(' ');
string userId = report[i].substr(0,spaceIdx);
string reportedId = report[i].substr(spaceIdx+1);
reportedNums[reportedId]++;
reportingIds[userId].push_back(reportedId);
}
유저가 신고한 ID들 중 신고당한 횟수가 k번 이상인 ID의 개수(정지된 ID의 개수, 정지 메일 수신 횟수)를 answer배열에 업데이트한다.
for (string userId:id_list){
int cnt=0;
for (string reportingId:reportingIds[userId]){
if (reportedNums[reportingId] >=k) cnt++;
}
answer.push_back(cnt);
}'Programmers' 카테고리의 다른 글
| [프로그래머스] 같은 숫자는 싫어 (JS) (0) | 2023.11.12 |
|---|---|
| [프로그래머스] 가장 큰 수 (JS) - sort (0) | 2023.11.11 |
| [프로그래머스] K번째수 (JS) - sort (0) | 2023.11.10 |
| [프로그래머스] 소수 찾기 (JS) - 완전탐색, DFS (1) | 2023.11.09 |
| [프로그래머스] 모의고사 (JS) - 완전탐색 (0) | 2023.11.09 |