๐Ÿ”Žํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋ฒ ์ŠคํŠธ์•จ๋ฒ”

๋ฐ•๋ฏผ์šฐยท2023๋…„ 7์›” 10์ผ
0
post-custom-banner

๋ฌธ์ œ: https://school.programmers.co.kr/learn/courses/30/lessons/42579

์นดํ…Œ๊ณ ๋ฆฌ: ํ•ด์‹œ

์ถœ์ฒ˜: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๊ณ ๋“์  Kit, https://school.programmers.co.kr/learn/challenges?tab=algorithm_practice_kit


โœ๏ธ ๋‚ด ํ’€์ด

  1. ๊ฐ ์žฅ๋ฅด๋ณ„ ์ด ์žฌ์ƒ ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ํ•ด์‹œ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ ๋‹ค.

    genreTotalPlay : { classic: 1450, pop: 3100}
  2. ๊ฐ ์žฅ๋ฅด์— ์†ํ•œ ๋…ธ๋ž˜์™€ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•˜๋Š” ํ•ด์‹œ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ ๋‹ค.

    genreTable : {classic: [ [500, 0], [150, 2], [800, 3] ], pop: [ ~]}
  3. 2์—์„œ ๋งŒ๋“  ํ•ด์‹œ ํ…Œ์ด๋ธ”์— ์†ํ•œ ๊ฐ ์žฅ๋ฅด์˜ ๋…ธ๋ž˜๋“ค์„ ์žฌ์ƒ์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ , ์ตœ๋Œ€ 2๊ฐœ๋งŒ ์„ ํƒํ•œ๋‹ค.

    genreTable : {classic: [ [ 800, 3 ], [ 500, 0 ] ], pop: [ [ 2500, 4 ], [ 600, 1 ] ]}
  4. ์ด ์žฌ์ƒ ์ˆ˜๊ฐ€ ๋†’์€ ์žฅ๋ฅด ์ˆœ์œผ๋กœ ์žฅ๋ฅด ์ด๋ฆ„ ์ •๋ ฌํ•ด์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑํ•œ๋‹ค.

    keyArr : ["pop", "classic"] 
  5. keyArr์˜ ๊ฐ ์žฅ๋ฅด ์ด๋ฆ„์„ key๋กœ ๊ฐ€์ง€๋Š” genreTable์˜ value ์ค‘ index๋งŒ ์ด์–ด๋ถ™์ธ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.

    answer : [4,1,3,0]

๐Ÿ—’๏ธ ๋‚ด ์ฝ”๋“œ

function solution(genres, plays) {

  // ๊ฐ ์žฅ๋ฅด๋ณ„ ์ด ์žฌ์ƒ ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ํ•ด์‹œ ํ…Œ์ด๋ธ” genreTotalPlay :  { classic: 1450, pop: 3100}
  const genreTotalPlay = new Map();
  genres.forEach((genre, index) => {
    if (genreTotalPlay.get(genre)) {
      // table์— ์žฅ๋ฅด๊ฐ€ ๋“ฑ๋ก๋˜์–ด์žˆ๋‹ค๋ฉด
      const sum = genreTotalPlay.get(genre);
      genreTotalPlay.set(genre, sum + plays[index]);
    } else {
      genreTotalPlay.set(genre, plays[index]);
    }
  });

  // ๊ฐ ์žฅ๋ฅด์— ์†ํ•œ ๋…ธ๋ž˜์™€ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•˜๋Š” ํ•ด์‹œ ํ…Œ์ด๋ธ” gerneTable :  {classic: [ [500, 0], [150, 2], [800, 3] ], pop: []}
  const genreTable = new Map();
  genres.forEach((gerne, index) => {
    if (genreTable.get(gerne)) {
      genreTable.set(gerne, [...genreTable.get(gerne), [plays[index], index]]);
    } else {
      genreTable.set(gerne, [[plays[index], index]]); // [500, 0] ์ด๋ ‡๊ฒŒ ์žฌ์ƒ์ˆ˜์™€ index๋ฅผ ํ•จ๊ป˜ ์ €์žฅ 
    }
  });
  
  // ๊ฐ ์žฅ๋ฅด์— ์†ํ•œ ๋…ธ๋ž˜๋“ค์„ ์žฌ์ƒ์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ , ์ตœ๋Œ€ 2๊ฐœ๋งŒ ์„ ํƒํ•˜๊ธฐ
   // 'classic' => [ [ 800, 3 ], [ 500, 0 ] ],
  // 'pop' => [ [ 2500, 4 ], [ 600, 1 ] ]
  genreTable.forEach((arr, key) => {
    const newArr = arr.sort((a, b) => {
      return b[0] - a[0];
    });

    if (newArr.length <= 2) {
      genreTable.set(key, newArr);
    } else {
      genreTable.set(key, newArr.slice(0, 2));
    }
  });

  // ์ด ์žฌ์ƒ ์ˆ˜๊ฐ€ ๋†’์€ ์žฅ๋ฅด ์ˆœ์œผ๋กœ ์žฅ๋ฅด ์ด๋ฆ„ ์ •๋ ฌ
  const keyArr = [...genreTotalPlay.keys()].sort((a, b) => {
    return genreTotalPlay.get(b) - genreTotalPlay.get(a);
  });

  // ์ •๋ ฌ๋œ ์žฅ๋ฅด ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ๋…ธ๋ž˜๋“ค์˜ ์ธ๋ฑ์Šค ์ด์–ด ๋ถ™์ด๊ธฐ
  let answer = [];
  keyArr.forEach((key) => {
    genreTable.get(key).forEach((element) => answer.push(element[1]));
  });

  return answer;
}

solution(
  ["classic", "pop", "classic", "classic", "pop"],
  [500, 600, 150, 800, 2500]
);

โœ’๏ธ Solution ๋ฐ ๊ฐœ์„ ๋ฐฉ์•ˆ

ํ’€๊ธด ํ–ˆ์ง€๋งŒ, ๋‚ด ์ฝ”๋“œ์—๋Š” ๋ญ”๊ฐ€ ๋ถˆํ•„์š”ํ•œ ๋กœ์ง์ด ์žˆ์–ด ๋จผ ๊ธธ์„ ๋Œ์•„ ํ‘ผ ๊ฒƒ ๊ฐ™์•˜๊ณ , ๋” ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„ ๊ฒƒ ๊ฐ™์•˜๋‹ค.

๋‹ค์Œ์€ solution ์ฝ”๋“œ์ด๋‹ค.

function solution(genres, plays) {
  const genreMap = new Map();

  genres
    .map((genre, index) => {
      return [genre, plays[index]];
    }) // [classic, 500], [pop, 600] ...
    .forEach(([genre, play], index) => {
      // [classic, 500, 0] ๋น„ ๊ตฌ์กฐํ™” ํ• ๋‹น

      // ๊ธฐ์กด ์žฅ๋ฅด์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ
      const data = genreMap.get(genre) || { total: 0, songs: [] }; // ์žฅ๋ฅด๊ฐ€ ์ฒ˜์Œ ๋“ฑ๋ก๋  ๋•Œ๋Š” undefined๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๋ฏ€๋กœ, ์ด๊ฒƒ ์ฒ˜๋ฆฌ

      genreMap.set(genre, {
        total: data.total + play,
        songs: [...data.songs, { play, index }] // ๊ธฐ์กด์˜ songs๊ฐ€ [{500, 0}, {150,2} ...] ์ด๋ ‡๊ฒŒ ์žˆ์„ ๊ฒƒ์ด๊ณ , ์—ฌ๊ธฐ๋‹ค๊ฐ€ ํ˜„์žฌ์˜ {play, index}๋ฅผ ์ถ”๊ฐ€ํ•ด์ค„ ๊ฒƒ
          .sort((a, b) => b.play - a.play) // ์žฌ์ƒ ์ˆ˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ 2๊ฐœ๋งŒ ์งค๋ผ์„œ ์ €์žฅ
          .slice(0, 2),
      });
      //  genremap : {
      //    classic: { total: 1450 , songs: [{ play: 800, index: 3 }, { play: 500, index: 0 } ] },
      //    pop : { total: 3100, songs: [{ play: 2500, index: 4 }, { play: 600, index: 1 } ] }
      //  }
      //
    });

  return [...genreMap.entries()] // [  [ 'classic', { total: 1450, songs: [Array] } ], [ 'pop', { total: 3100, songs: [Array] } ] ]
    .sort((a, b) => b[1].total - a[1].total) 
    .flatMap((item) => item[1].songs) // [ { play: 2500, index: 4 }, { play: 600, index: 1 }, { play: 800, index: 3 }, { play: 500, index: 0 } ]
    .map((song) => song.index);
}
  • ์ฒ˜์Œ ์ฃผ์–ด์ง„ genres ๋ฐฐ์—ด๊ณผ plays๋ฅผ ์กฐํ•ฉํ•ด, ๊ฐ ์žฅ๋ฅด๋ณ„ ์ด ์žฌ์ƒ ์ˆ˜total๊ณผ ๋…ธ๋ž˜ ๋ชฉ๋ก songs๋ฅผ ํฌํ•จํ•œ ํ•ด์‹œํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด ๋†“๊ณ  ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์œผ๋กœ ๋Š๊ปด์กŒ๋‹ค. ์ด๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์—์„œ ์ตœ๋Œ€ ์žฌ์ƒ ๋œ ๋…ธ๋ž˜ 2๊ฐœ๋งŒ ์„ ํƒํ•˜๋Š” ๊ณผ์ •๋„ ํฌํ•จ๋๋‹ค.

    genremap : {
        classic: { total: 1450 , songs: [{ play: 800, index: 3 }, { play: 500, index: 0 } ] },
        pop : { total: 3100, songs: [{ play: 2500, index: 4 }, { play: 600, index: 1 } ] }
    }
  • ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž || (or)์„ ์ด์šฉํ•ด data์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•ด์ฃผ๋Š” ๊ธฐ๋ฒ•๋„ ์ž˜ ์•Œ์•„๋†”์•ผ๊ฒ ๋‹ค.

     const data = genreMap.get(genre) || { total: 0, songs: [] };
  • flatMap() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ฐ ์š”์†Œ๋“ค์˜ ๊นŠ์ด๋ฅผ -1์”ฉ ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

profile
๊พธ์ค€ํžˆ, ๊นŠ๊ฒŒ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€