반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

undefined

[프로그래머스] 신고 결과 받기 -JS 본문

Coding Test

[프로그래머스] 신고 결과 받기 -JS

JavaScripter 2022. 7. 2. 02:31
반응형

문제 설명

 

// 신고횟수 제한 X
// 동일유저신고 1회로
// k번신고되면 정지
// 취합후 마지막에 한꺼번에 정지
// 정지메일 발송횟수 리턴


문제 풀이

function solution(id_list, reports, k) {
    const reportCount = new Array(id_list.length).fill(0)
    const reportList = {}
    
    id_list.map(id=>{
        reportList[id] = []
    })
    
    for(let report of reports) {
         const [userId,reportedId] =report.split(' ')
         if(!reportList[reportedId].includes(userId)){
             reportList[reportedId].push(userId)
         }
    }
    id_list.map(id => {
        if(reportList[id].length>=k){
        reportList[id].map(e=>{
            reportCount[id_list.indexOf(e)]++
        })
        }
    })
    
           return reportCount
    
}

1. 리턴할 0,0,0,0 배열 만들기

 

2. 키 값 부여를 위한 reportList 객체 생성

 

3. id_list를 맵핑하여 list의 데이터로 키값과 빈배열 반복하여 만들어주기

 

4. 객체를 distructing하여 각각 userId와 reportedId로 정의

 

5. 각 유저당 신고받은 배열을 뽑기위하여 신고받은 유저를 키값으로 하여 그 안에 신고한 유저 넣기 (단, 여러번 신고해도 1번으로 카운팅이므로 배열에 포함되지 않았을 때만 넣기)

 

6. 신고받은 유저의 배열이 k값보다 크거나 같으면 id_list에서의 신고받은 유저 index를 뽑아 위에 만들어 두었던 reportCount의 해당 인덱스에 카운트+1


배운 점

변하는 데이터의 경우에도 map을 이용하여 객체에 요소를 추가할 수 있다.

 

배열 디스트럭팅의 활용

 

반응형
Comments