본문 바로가기
코테/코테withJS

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

by 트레일헤드레인저 2023. 4. 14.
SMALL

프로그래머스 Lv0 100문제를 다 풀어보고

프로그래머스 Lv1 77문제에서 본 문제포함 2문제를 남겨놨다.

이 문제부터 진짜라고 생각된다.

다른 방식으로 푼 사람들도 있지만(Set,Map)

그 중에서 가장 습득하고 싶은 풀이법을 보면서 공부했다. 

https://school.programmers.co.kr/learn/courses/30/lessons/92334

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

report[신고자, 신고당하는사람] ,

k이상신고당하면 정지

정지당하는 사람"을" 신고한 사람한테 메일(result)를 보내준다.

즉 내가 무지를 신고했는데 무지가 정지되면 나한테 "무지 정지됐어요~"라는 형태의 메일을 얼마나 받았는가를 물어보는 문제


function solution(id_list, report, k) {
    var answer = [];
    const users = [];

    // 유저의 정보요소 설정
    for (const id of id_list) {
        users[id] = { reportedBy: [], reportTo: [], ban: false, success: 0 }
    }

    // 신고내역 정리
    for (const re of report) {
        const [reporter, reported] = re.split(" ")
        if (!users[reporter].reportTo.includes(reported)) users[reporter].reportTo.push(reported)
        if (!users[reported].reportedBy.includes(reporter)) users[reported].reportedBy.push(reporter)
        if (users[reported].reportedBy.length >= k) {
            users[reported].ban = true
        }
    }

    // 정지먹은 사람들을 신고한 유저에게 메일 발송
    for (const user in users) {
        if (users[user].ban) {
            for (const reporter of users[user].reportedBy) {
                users[reporter].success++;
            }
        }
    }

    //정지 성공횟수 모음
    for (const user in users) {
        answer.push(users[user].success)
    }

    return answer;
}

 

얼핏 코드를 보면 할만한데? 라고생각이되지만 처음 유저의 정보요소 설정부분이 낯설어 헤맸다.

현재프로그래머스에서 두번째로 낮은정답률

 

LIST

'코테 > 코테withJS' 카테고리의 다른 글

JS 프로그래머스 Lv2 숫자의표현  (0) 2023.04.23
백준 Node.js 2606 바이러스  (0) 2023.01.01
백준 Node.js 1388 타일뭐시기  (0) 2022.12.26
백준 Node.js 16173 쩰리뭐시기  (0) 2022.12.26
유용한 method(배열)  (0) 2022.11.03