[프로그래머스] 신고 결과 받기 (2022 KAKAO BLIND RECRUITMENT)

2023. 1. 3. 16:43기록/Programmers

    목차

문제 링크

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

 

프로그래머스

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

programmers.co.kr


나의 문제 풀이

function solution(id_list, report, k) {
  const answer = [];
  const reportList = {};
  const mailCount = {};

  id_list.forEach((id) => {
    reportList[id] = [];
    mailCount[id] = 0;
  });

  report = [...new Set(report)];
  report.forEach((item) => {
    const [from, to] = item.split(' ');
    reportList[to].push(from);
  });

  for (let key in reportList) {
    if (reportList[key].length >= k) {
      reportList[key].forEach((id) => {
        mailCount[id] = mailCount[id] ? mailCount[id] + 1 : 1;
      });
    }
  }

  for (let key in mailCount) {
    answer.push(mailCount[key]);
  }

  return answer;
}
  • reportList
    • key : 신고당한 사람
    • value : 신고한 사람들 배열
  • mailCount
    • key : 신고한 사람
    • value: 메일 받을 횟수
  • 주어진 id 리스트 배열을 이용해 두 객체의 key, value 초기화
  • 주어진 report 배열에서 중복을 제거한 후 요소를 하나씩 꺼냄
    • 현재 요소를 구조 분해 할당하여 신고한 사람, 신고당한 사람 변수에 저장
    • reportList 객체에 신고당한 사람을 key 값으로, 신고한 사람을 value 값인 배열의 요소로 넣음
  • reportList 객체의 key를 하나씩 꺼냄 (신고당한 사람)
    • 현재 key 값으로 찾은 배열의 길이가 주어진 k보다 크거나 같을 경우 (이용 정지 대상)
    • 해당 배열의 요소들을 하나씩 꺼냄
    • mailCount 객체에서 해당 요소를 key로 찾은 값이 있을 경우 그 값 + 1, 없을 경우 1을 넣어줌
  • mailCount 객체의 key를 하나씩 꺼냄 (신고한 사람)
    • 현재 key 값으로 찾은 값을 정답 변수 배열에 push 함 (메일 받을 횟수)

다른 사람의 문제 풀이

function solution(id_list, report, k) {
  let reports = [...new Set(report)].map((a) => 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;
}