[Lv.1]신고 결과 받기

Jihyun-Jeon·2022년 3월 13일
0

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

🔶 내가 푼 것

  • 시간 초과되서 실패함
  1. report에서 중복값 제거함
  2. report를 이중배열로 만듦
  3. report를 돌면서 {키(신고당한 사람): 값(신고한 사람 배열로)} 객체를 만듦
  4. 그 값인 배열의 길이가 k이상 이라면, 그 배열을 다 모음
  5. 해당 이름이 몇 번 있는지 카운트한 결과를 반환함
function solution(userList, report, k) {
  // 1. report 중복값 제거
  const newArr = report.filter((el, idx) => report.indexOf(el) === idx);

  // 2.이중배열로 만듦
  const reportArr = [];
  newArr.forEach((el) => {
    reportArr.push(el.split(' ')); // [['muzi', 'frodo'],['apeach', 'frodo'],[],[],[] ]
  });

  // 3.객체에 {키(신고당한 사람): 값(신고한 사람 배열로)} 객체를 만듦
  const obj = {}; // {frodo: ['muzi','apeach'] , muzi: ['apeach'], neo:['frodo', 'muzi']}
  reportArr.forEach((el) => {
    if (!obj[el[1]]) {
      // 키값이 없으면
      obj[el[1]] = [el[0]];
    } else {
      // 이미 있으면
      obj[el[1]] = [...obj[el[1]], el[0]];
    }
  });
  
  // 4.메일 받는 사람만 추출 - 객체의 값이 배열인데, 길이가 k이상이면 그 배열을 다 모음
  let mail = []; // ['muzi', 'apeach', 'frodo', 'muzi']
  for (const el in obj) {
    if (obj[el].length >= k) {
      mail = [...mail, ...obj[el]];
    }
  }
  // 5.해당 값 몇 개 있는지 카운트
  const result = [];
  userList.forEach((el) => {
    let cnt = 0;
    for (let i = 0; i < mail.length; i += 1) {
      if (el === mail[i]) {
        cnt += 1;
      }
    }
    result.push(cnt);
  });
  return result;
}

// 실행1
console.log(
  solution(
    ['muzi', 'frodo', 'apeach', 'neo'],
    [
      'muzi frodo',
      'muzi frodo',
      'apeach frodo',
      'frodo neo',
      'muzi neo',
      'apeach muzi',
    ],
    2,
  ),
); // [2, 1, 1, 0],
// 실행2
console.log(
  solution(
    ['con', 'ryan'],
    ['ryan con', 'ryan con', 'ryan con', 'ryan con'],
    3,
  ),
); // [0, 0],

🔶 다른 사람 풀이

  1. Set,Map이용
function solution(idList, report, k) {
  // <1.report를 이중배열로 만듦>
  const reports = [...new Set(report)].map((el) => el.split(' '));
  // console.log([...new Set(report)]); // ['muzi frodo', 'apeach frodo', 'frodo neo', 'muzi neo', 'apeach muzi']
  // reports = [ ['muzi', 'frodo'],['apeach', 'frodo'], 'frodo', 'neo'],['muzi', 'neo'], ['apeach', 'muzi']]

  // <2.신고당한 사람 찾음>
  const counts = new Map(); // counts = {'frodo' => 2, 'neo' => 2, 'muzi' => 1}
  for (const bad of reports) {
    counts.set(bad[1], counts.get(bad[1]) + 1 || 1);
  }

  // <3.신고해서 메일 받는사람 카운트>
  const good = new Map(); // good = {'muzi' => 2, 'apeach' => 1, 'frodo' => 1}
  for (const report of reports) {
    if (counts.get(report[1]) >= k) {
      good.set(report[0], good.get(report[0]) + 1 || 1);
    }
  }

  const result = idList.map((el) => good.get(el) || 0);
  return result;
}

🔶 피드백

  1. Set,Map 공부하기
    : deep dive 해당부분 읽음
  2. 배열에 중복값 제거하는 법
const arr = [
  'muzi frodo',
  'muzi frodo',
  'apeach frodo',
  'frodo neo',
  'muzi neo',
  'apeach muzi',
];

// <방법1. filter indexOf 이용  >
// indexOf는 첫 번째 인덱스를 반환하고 존재하지 않으면 -1을 반환하는 특성을 이용.
const newArr = arr.filter((el, idx) => arr.indexOf(el) === idx);
console.log(newArr);

// <방법2. Set, from 이용>
// Set은 중복값 포함할 수 없음
// Array.from : 유사 배열 객체나 iterable 객체를 얕게 복사해 새로운 배열로 만듦.
const newArr = Array.from(new Set(arr));
console.log(newArr);

0개의 댓글