-
[프로그래머스] 신고 결과 받기 자바스크립트(Javascript)알고리즘(algorithm)/프로그래머스 2022. 3. 1. 16:26
문제
https://programmers.co.kr/learn/courses/30/lessons/92334
코딩테스트 연습 - 신고 결과 받기
문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의
programmers.co.kr
풀이
function solution(id_list, report, k) { var answer = Array.from({ length: id_list.length }, () => 0); const set = new Set(report); const newReport = [...set]; // set을 통하여 중복을 제거한다. const reportArray = {}; const reportedIds = []; id_list.map((id) => { reportArray[id] = []; }); newReport.reduce((acc, cur) => { const [who, reported] = cur.split(' '); acc[reported] = (acc[reported] || 0) + 1; if (!reportArray[who].includes(reported)) reportArray[who].push(reported); if (acc[reported] >= k && !reportedIds.includes(reported)) reportedIds.push(reported); return acc; }, {}); // 신고를 당한 사람이 몇명인지 계산하면서, k 이상이면 reportedIds에 추가한다. let i = 0; for (const [id, num] of Object.entries(reportArray)) { reportedIds.map((id) => { if (num.includes(id)) { answer[i]++; } }); i++; } return answer; }
'알고리즘(algorithm) > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 실패율 자바스크립트(Javascript) (0) 2022.03.01 [프로그래머스] 프렌즈 4블록 자바스크립트(Javascript) (0) 2022.01.10 [프로그래머스] 파일명 정렬 자바스크립트(Javascript) (0) 2022.01.07 [프로그래머스] 카카오 다트 게임 자바스크립트(Javascript) (0) 2021.11.26 [프로그래머스] 다리를 지나는 트럭 자바스크립트(Javascript) (0) 2021.11.26