티스토리 뷰

알고리즘/프로그래머스 문제풀이

프로그래머스 NodeJS Level 1 : 신고 결과 받기

YG - 96년생 , 강아지 있음, 개발자 희망 2022. 3. 3. 20:19
/* get input */
let id_list = ["muzi", "frodo", "apeach", "neo"];
let report = [
  "muzi frodo",
  "apeach frodo",
  "frodo neo",
  "muzi neo",
  "apeach muzi",
];
let k = 2;
/* get input end */

/* solve */
function solution(id_list, report, k) {
  var answer = [];

  let countReport = [];
  for (let i = 0; i < id_list.length; i++) {
    countReport.push({ id: id_list[i], count: 0, reportUser: [] });
  }

  for (let i = 0; i < report.length; i++) {
    let users = report[i].split(" ");

    const findIndex = countReport.findIndex((item) => item.id === users[0]);
    const reportFindIndex = countReport.findIndex(
      (item) => item.id === users[1]
    );
    if (countReport[findIndex].reportUser.indexOf(users[1]) < 0) {
      countReport[findIndex].reportUser.push(users[1]);
      countReport[reportFindIndex].count++;
    }
  }

  for (let i = 0; i < id_list.length; i++) {
    let howMuch = 0;
    for (let j = 0; j < countReport[i].reportUser.length; j++) {
      let who = countReport[i].reportUser[j];
      const findIndex = countReport.findIndex((item) => item.id === who);
      if (countReport[findIndex].count >= k) {
        howMuch++;
      }
    }
    answer.push(howMuch);
  }

  return answer;
}
/* solve end */

/* print output */
console.log(solution(id_list, report, k));
/* print output end */

findIndex를 이용해서 대부분 풀었으며 새로운 배열을 만들어 거기서 신고한 아이디 , 신고당한 횟수  신고한 유저 등등 여러 정보를 만들어 넣은 후 이 정보가 담긴 오브젝트를 이용해서 문제를 풀었습니다. 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   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
글 보관함