๐Ÿ’ป ํ•ด์‹œ ํ…Œ์ด๋ธ” by ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์—ฐ์Šต๋ฌธ์ œ

waterglassesยท2022๋…„ 3์›” 24์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
4/50
post-thumbnail

์˜ค๋Š˜์€ ํ, ํ•ด์‹œ ํ…Œ์ด๋ธ”, ๊ทธ๋ž˜ํ”„์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜์˜€๊ณ  ๋น„์Šทํ•˜๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋˜ ํ์™€ ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ๋ณด๋‹ค ๋ชจ๋ฒ” ๋‹ต์•ˆ(๊ฐ•์˜)์— ์ถฉ๊ฒฉ๋จน์—ˆ๋˜ ํ•ด์‹œํ…Œ์ด๋ธ”์„ ์ง‘์ค‘์ ์œผ๋กœ ๋ด๋ณด์ž!

๐Ÿ“ ํ•ด์‹œ ํ…Œ์ด๋ธ”

๋ฒ ์ŠคํŠธ ์•จ๋ฒ”

์ถœ์ฒ˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
๋ฒ ์ŠคํŠธ ์•จ๋ฒ”

๋‚ด ๋‹ต์•ˆ

function solution(genres, plays) {
  const genreObj = {};
  for (let i = 0; i < genres.length; i++) {
    if (!genreObj.hasOwnProperty(genres[i])) {
      genreObj[genres[i]] = {
        total: plays[i],
        plays: [[i, plays[i]]],
      };
    } else {
      genreObj[genres[i]].total += plays[i];
      genreObj[genres[i]].plays.push([i, plays[i]]);
    }
  }

  const sortGenre = [];
  for (const genre in genreObj) {
    genreObj[genre].plays.sort((a, b) => b[1] - a[1]);
    sortGenre.push([genre, genreObj[genre].total, genreObj[genre].plays]);
  }
  sortGenre.sort((a, b) => b[1] - a[1]);

  const bestAlbum = [];
  for (const genre of sortGenre) {
    genre[2].forEach((val, idx) => {
      if (idx < 2) {
        bestAlbum.push(val[0]);
      }
    });
  }
  return bestAlbum;
}

๐Ÿ’ก๊ตฌํ˜„ ๋ฐฉ๋ฒ•

  1. ๊ฐ™์€ ์žฅ๋ฅด๋ผ๋ฆฌ ๋ฌถ๊ธฐ ์œ„ํ•ด์„œ object๋ฅผ ์‚ฌ์šฉํ•˜์˜€๊ณ  ์žฅ๋ฅด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด total์— ์ด ํ”Œ๋ ˆ์ด ์ˆ˜, plays์—๋Š” ์ธ๋ฑ์Šค์™€ ํ”Œ๋ ˆ์ด ์ˆ˜๋ฅผ ์„ค์ •ํ•˜์˜€๋‹ค.
  2. ๊ฐ ์žฅ๋ฅด์˜ play์ˆ˜๋ฅผ ์ •๋ ฌํ•˜์˜€๊ณ  ๊ฐ€์žฅ ๋งŽ์ด ์žฌ์ƒ๋œ ์žฅ๋ฅด๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด sortGenre array๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.
  3. ์ด ์žฅ๋ฅด์ˆ˜๋กœ ์žฅ๋ฅด๋ฅผ ์ •๋ ฌํ•˜๊ณ , ์žฅ๋ฅด๋งˆ๋‹ค ๋…ธ๋ž˜๋ฅผ 2๊ฐœ๊นŒ์ง€ ์ˆ˜๋กํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— idx๊ฐ€ 2๊ฐ€ ๋˜๊ธฐ ์ „๊นŒ์ง€์˜ ๋…ธ๋ž˜์˜ ๋ฒˆํ˜ธ๋ฅผ ๋ฒ ์ŠคํŠธ ์•จ๋ฒ”์— ์ถ”๊ฐ€์‹œ์ผฐ๋‹ค.

๋ชจ๋ฒ” ๋‹ต์•ˆ

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

  genres
    .map((genre, index) => [genre, plays[index]])
    .forEach(([genre, play], index) => {
      const data = genreMap.get(genre) || { total: 0, songs: [] };
      genreMap.set(genre, {
        total: data.total + play,
        songs: [...data.songs, { play, index }]
          .sort((a, b) => b.play - a.play)
          .slice(0, 2),
      });
    });

  return [...genreMap.entries()]
    .sort((a, b) => b[1].total - a[1].total)
    .flatMap((item) => item[1].songs)
    .map((songs) => songs.index);
}

๐Ÿ”ฅ ๋Š๋‚€์ 

๐Ÿ˜ฑ ํ•จ์ˆ˜์˜ ํ™œ์šฉ.. ์žฅ๋‚œ์•„๋‹ˆ๋‹ค.. ์ถฉ๊ฒฉ์ด๋ž„๊นŒ ๐Ÿคฏ
์†”์งํžˆ ์ฒ˜์Œ์— ์•„ ์ด์ •๋„๋ฉด ๊ดœ์ฐฎ๊ฒŒ ํ’€์—ˆ๋‹ค ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ..
์ด๊ฒŒ ๋ญ๋žŒ! ์ถฉ๊ฒฉ์ ์ด๋‹ค..

const data = genreMap.get(genre) || { total: 0, songs: [] };

์šฐ์„  ์ด ๋ถ€๋ถ„! ๋ฌธ์ œ ํ’€ ๋•Œ ์‚ฌ์šฉํ•ด ๋ณผ ์ƒ๊ฐ์„ ์ „ํ˜€ ํ•ด๋ณด์ง€ ์•Š์•˜๋‹ค. if, else์™€ Map์—์„œ has๋กœ ์กด์žฌ ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์„œ ์ฝ”๋”ฉํ–ˆ์—ˆ๋Š”๋ฐ..๐Ÿ˜‚ ์ •๋ง ์ต์ˆ™ํ•œ ๋ฌธ๋ฒ•์ธ๋ฐ ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋‹ค๋‹ˆ! ์ด์ œ ๋‹ค์Œ๋ถ€ํ„ฐ ์ด๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ์ ‘ํ•˜๋ฉด ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋ด์•ผ์ง€!๐Ÿ˜€

  return [...genreMap.entries()]
    .sort((a, b) => b[1].total - a[1].total)
    .flatMap((item) => item[1].songs)
    .map((songs) => songs.index);

์ •๋ง ๊น”๋”ํ•œ ํ’€์ด ์•„๋‹Œ๊ฐ€.. [...genreMap.entries()] Map์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ž์ฃผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ๋ฌธ๋ฒ•์ด๋‹ค. ๋‹ค์Œ์— ๊ผญ ์‚ฌ์šฉํ•ด๋ด์•ผ์ง€~

flapMap ์ฒ˜์Œ ๋“ค์–ด๋ณธ ํ•จ์ˆ˜์ด๋‹ค. ๊ณ ๋กœ ์ •๋ฆฌํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

๐Ÿ’ก flat()๊ณผ flatMap()

flat()

์ค‘์ฒฉ๋œ ๋ฐฐ์—ด ๊ตฌ์กฐ๋ฅผ ์ •ํ•ด์ง„ ๊นŠ์ด๋งŒํผ ํ‰ํƒ„ํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜์ด๋‹ค.
๊นŠ์ด์˜ default๋Š” 1์ด๋ฉฐ ๋”ฐ๋กœ ์ง€์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

const arr1 = [1, 2, [3, 4]];
arr1.flat(); 
// [1, 2, 3, 4]

const arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat(2);
// [1, 2, 3, 4, 5, 6]

const arr3 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
arr3.flat(Infinity);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

flatMap()

๋ฐฐ์—ด์„ ํ‰ํƒ„ํ™”ํ•˜๋Š” flat()์˜ ๊ธฐ๋Šฅ์— map()์˜ ๊ธฐ๋Šฅ์„ ํ•ฉ์นœ ํ•จ์ˆ˜์ด๋‹ค.

let arr1 = ["it's Sunny in", "", "California"];

arr1.map(x=>x.split(" "));
// [["it's","Sunny","in"],[""],["California"]]

arr1.flatMap(x => x.split(" "));
// ["it's","Sunny","in","California"]

Refer

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด๋‚ด์žฅํ•จ์ˆ˜ ์ •๋ฆฌ (2)

์˜ค๋Š˜์˜ ๋‚ด์šฉ ์ •๋ฆฌ

๋ฐ๋ธŒ์ฝ”์Šค Day4

profile
๋งค ์ˆœ๊ฐ„ ์„ฑ์žฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

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

comment-user-thumbnail
2022๋…„ 3์›” 24์ผ

์†”์งํžˆ ์ฒ˜์Œ์— ์•„ ์ด์ •๋„๋ฉด ๊ดœ์ฐฎ๊ฒŒ ํ’€์—ˆ๋‹ค <<< ๊ณต๊ฐํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ค ๐Ÿ˜‚๐Ÿ˜‚

1๊ฐœ์˜ ๋‹ต๊ธ€
comment-user-thumbnail
2022๋…„ 3์›” 24์ผ

์ง„์งœ ๊ทธ ๋ฌธ์ œ ํ’€๊ณ  ์Šค์Šค๋กœ์— ๋Œ€ํ•œ ๋ฐ˜์„ฑ์ด ๋“ค์—ˆ์–ด์š”
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‹ต๊ฒŒ ์“ฐ๋Š”๊ฑด ์ €๋Ÿฐ๊ฑฐ๊ตฌ๋‚˜.. ์‹ถ์€
์–ธ์–ด๋ฅผ ์“ธ ๋•Œ๋Š”, ์–ธ์–ด์˜ ๋ณธ์งˆ์„ ํŒŒ์•…ํ•˜๊ณ  ํŠน์„ฑ์— ๋งž๊ฒŒ ์‚ฌ์šฉํ•˜๋ผ๋Š” ๋ง์„ ๋งŽ์ด ๋“ค์—ˆ๋Š”๋ฐ,
์ด๋ž˜์„œ ๊ณ ์ˆ˜๋“ค์˜ ์ฝ”๋“œ๋ฅผ ๋งŽ์ด ๋ด์•ผ๋˜๊ตฌ๋‚˜- ์‹ถ์—ˆ๋˜ ์‹œ๊ฐ„์ด์˜€๋„ค์š”...

1๊ฐœ์˜ ๋‹ต๊ธ€