코딩테스트 연습
2022 KAKAO BLIND RECRUITMENT
신고 결과 받기
객체로 이뤄진 배열에서 value 찾기 : find, findIndex
문자열 또는 단순 list에서 설정한 조건 찾기 : indexOf, search
function solution(id_list, report, k) {
var answer = [];
let list=[];
id_list.forEach(function(mem){
list.push({name:mem,cnt:0,reporter:""})
//reporter를 문자열로 둠
answer.push(0);
})
report.forEach(function(rep){
let mem=rep.split(" ")
let idx=list.findIndex(e=>e.name===mem[1]);
if(list[idx].reporter.search(mem[0])===-1){
list[idx].reporter+=mem[0]+" ";
//띄어쓰기로 신고자 구분
list[idx].cnt++;
}
})
list.forEach(function(obj){
if(obj.cnt>=k){
let memList=obj.reporter.split(" ")
memList.pop();
//빈 배열 삭제
memList.forEach(function(reporter){
let repIdx=list.findIndex(e=>e.name===reporter);
answer[repIdx]++;
})
}
})
return answer;
}
function solution(id_list, report, k) {
var answer = [];
let list=[];
id_list.forEach(function(mem){
list.push({name:mem,cnt:0,reporter:[]})
//reporter를 배열로 변경
answer.push(0);
})
report.forEach(function(rep){
let mem=rep.split(" ")
let idx=list.findIndex(e=>e.name===mem[1]);
if((list[idx].reporter).indexOf(mem[0])===-1){
list[idx].reporter.push(mem[0]);
list[idx].cnt++;
}
})
list.forEach(function(obj){
if(obj.cnt>=k){
let memList=obj.reporter;
memList.forEach(function(reporter){
let repIdx=list.findIndex(e=>e.name===reporter);
answer[repIdx]++;
})
}
})
return answer;
}
function solution(id_list, report, k) {
let reports = [...new Set(report)].map(a=>{return a.split(' ')});
let counts = new Map();
for (const bad of reports){
counts.set(bad[1],counts.get(bad[1])+1||1)
}
let good = new Map();
for(const report of reports){
if(counts.get(report[1])>=k){
good.set(report[0],good.get(report[0])+1||1)
}
}
let answer = id_list.map(a=>good.get(a)||0)
return answer;
}
set과 map 개념
용량이 작은 테스트케이스는 거의 비슷한 수행시간이 소요되었으나 큰 테스트케이스의 경우에는 시간이 5배 정도 가량 차이 남.