1시간 정도 고민했는데 결국 못풀었다.
level1 부터 이렇게 막히다니 앞으로가 걱정이 되지만 계속해보자
<미완성 풀이>
function solution(id_list, report, k) {
var answer = [];
// 중복 값 제거 = 여러번 신고한거 제거
var valid_report = [];
for(int i = 0; i<report.length; i++) {
if(report.indexOf(report[i])===i) {
valid_report.concat(report[i]);
}
}
// k 번 이상 신고받은 사람 구하기
var reported = [];
for(i in valid_report) {
var a = i.split(' ')[1]
reported.concat(a)
if(reported.includes(a))
}
if()
return answer;
}
<정답>
아래 포스팅을 참고하였습니다.
코딩테스트 - 신고 결과 받기 JavaScript
신고 결과 받기 프로그래머스 Lv1 JS 문제 풀이
velog.io
function solution(id_list, report, k) {
const answer = new Array(id_list.length);
answer.fill(0)
const report_list = {} //
id_list.map((user)=>{
report_list[user] = [] //key로 userid를 value로 빈 배열을 가지는 객체
})
report.map((user)=>{
const [user_id, report_id] = user.split(' ')
if(!report_list[report_id].includes(user_id)){
report_list[report_id].push(user_id)
}
})
for(const key in report_list){
if(report_list[key].length >= k){ //이용정지 유저
report_list[key].map((user)=>{
answer[id_list.indexOf(user)] += 1
})
}
}
return answer;
}
이렇게 간단하다니!!
핵심은
1) map 함수와 for in 문의 활용
2) 정답을 이끌어내기 위한 구조(형태)의 완성인 거 같다.
report_list = [user1: [신고자 id1, 신고자 id2...], user2: []....]
-> 이 형태를 기억하자
'주5일알고리즘' 카테고리의 다른 글
Programmers: 크레인 인형뽑기 게임 with JS (0) | 2022.07.05 |
---|---|
Programmers: 키패드 누르기 with JS (0) | 2022.07.04 |
Programmers: 숫자 문자열과 영단어 with JS (0) | 2022.07.01 |
Programmers: 신규 아이디 추천 with JS (1) | 2022.06.30 |
Programmers: 로또의 초고 순위와 최저 순위 with JS (0) | 2022.06.28 |