❓문제 설명
- 신고한 array와 신고 내용에 대한 array가 있고 limit을 나타내는 k가 있다
- 이러쿵 저러쿵해서 A를 신고한 B는 A가 k이상의 신고를 받게 되면 A가 정지가 되었다는 알림을 받는 것임
- 최종적으로 사용자들이 몇번이나 알림을 받는지 array를 반환하는 것임
- 테스트 코드는 통과하였지만 제출할 때 정답률 60%로 나오는데 왜 이러는걸까? - 느낀점 1
✅ 문제 해결
- 배열을 value로 가지고 key는 reportedUser인 객체 하나를 만들어서 배열에 신고한 reportUser를 넣을 것임
- 결국 신고를 받은 유저의 배열의 길이가 2이상이면 그 배열 안을 돌면서 값을 올려주면 되는 것임
function solution(id_list, report, k) {
const answer = new Array(id_list.length).fill(0);
const obj = {}
id_list.map(user => obj[user] = [])
report.map(item => {
const [report, reported] = item.split(" ")
if (!obj[reported].includes(report)) {
obj[reported].push(report)
}
})
for (key in obj) {
if (obj[key].length >= k) {
obj[key].map(user => answer[id_list.indexOf(user)] += 1)
}
}
return answer;
}
❕느낀점
- 마지막에 문제에서 제공하는 k를 사용하지 않고 예제를 보면서 2를 넣어서 정확도가 8.3이 나왔는데 뭔가 잘못되었다 싶으면 주어진 값부터 사용했는지 확인하는 습관을 기르면 좋을 것 같다.
- 객체에 배열을 만들어서 반복문을 돌리는 것에 대해서 생각을 잘 하면 좋을 것 같다.