KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • LeetCode
  • codeup
  • C++
  • tree
  • 백준
  • 그래프
  • Level 1
  • 수학
  • 다이나믹 프로그래밍
  • Level 0
  • Level1
  • string
  • js
  • 문자열
  • 정렬
  • Level 2
  • 제코베 JS 100제
  • recursion
  • programmers
  • C

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/Programmers

Programmers / Level 3 / 베스트앨범 / JS

2024. 4. 15. 21:50

< 문제 바로가기 >

 

프로그래머스

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

programmers.co.kr

 

const infoObj = {};
const playOfGenre = {};
let result = [];

function solution(genres, plays) {
  const length = genres.length;

  /**
   * 곡별 정보들을 객체로 만드는 함수
   */
  const getInfo = () => {
    for (let i = 0; i < length; i++) {
      const genre = genres[i];
      const play = plays[i];

      infoObj[i] = { genre, play };
    }
  };

  /**
   * 장르별 재생 횟수를 구하는 함수
   */
  const getPlayOfGenres = () => {
    for (const { genre, play } of Object.values(infoObj)) {
      playOfGenre[genre] = playOfGenre[genre] + play || play;
    }
  };

  /**
   * 많이 재생된 장르를 먼저 수록하는 함수
   */
  const getSortedObjByMostPlayedGenre = () => {
    const sortedByMostPlay = Object.entries(playOfGenre).sort(
      (a, b) => b[1] - a[1]
    );

    for (const [genre, _] of sortedByMostPlay) {
      const sortedList = [];
      for (let i = 0; i < length; i++) {
        if (genre === genres[i]) {
          sortedList.push({ index: i, genre, play: plays[i] });
        }
      }
      // 장르 내에서 많이 재생된 노래 먼저 수록
      result = [
        ...result,
        ...sortedList.sort((a, b) => b.play - a.play).slice(0, 2),
      ];
    }
  };

  /**
   * 장르 내에서 재생 횟수가 같은 노래 중에서 고유 번호가 낮은 노래를 먼저 수록하는 함수
   */
  const getSortedObjByLowerIndexInSamePlayOfSameGenre = () => {
    result.sort((a, b) => {
      if (a.genre !== b.genre) {
        return 1;
      } else if (a.play === b.genre) {
        return a.index - b.index;
      }
    });
  };

  const getResult = () => {
    result = result.map((el) => el.index);
  };

  getInfo();
  getPlayOfGenres();
  getSortedObjByMostPlayedGenre();
  getSortedObjByLowerIndexInSamePlayOfSameGenre();
  getResult();

  return result;
}
저작자표시 (새창열림)

'PS > Programmers' 카테고리의 다른 글

Programmers / Level 2 / 롤케이크 자르기 / JS  (0) 2024.07.02
Programmers / Level 2 / 뒤에 있는 큰 수 찾기 / JS  (0) 2024.07.02
Programmers / Level 2 / 숫자의 표현 / JS  (0) 2023.06.24
Programmers / Level 2 / 괄호 변환 / JS  (0) 2023.02.11
    'PS/Programmers' 카테고리의 다른 글
    • Programmers / Level 2 / 롤케이크 자르기 / JS
    • Programmers / Level 2 / 뒤에 있는 큰 수 찾기 / JS
    • Programmers / Level 2 / 숫자의 표현 / JS
    • Programmers / Level 2 / 괄호 변환 / JS
    KimMinJun
    KimMinJun

    티스토리툴바