# ๐ป ํด์ ํ์ด๋ธ by ํ๋ก๊ทธ๋๋จธ์ค ์ฐ์ต๋ฌธ์ 

waterglassesยท2022๋ 3์ 24์ผ
0

## TIL

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

์ค๋์ ํ, ํด์ ํ์ด๋ธ, ๊ทธ๋ํ์ ๋ํด์ ๊ณต๋ถํ์๊ณ  ๋น์ทํ๊ฒ ๊ตฌํํ๋ ํ์ ๊ทธ๋ํ ๋ฌธ์ ๋ณด๋ค ๋ชจ๋ฒ ๋ต์(๊ฐ์)์ ์ถฉ๊ฒฉ๋จน์๋ ํด์ํ์ด๋ธ์ ์ง์ค์ ์ผ๋ก ๋ด๋ณด์!

### ๐ ํด์ ํ์ด๋ธ

#### ๋ฒ ์คํธ ์จ๋ฒ

์ถ์ฒ : ํ๋ก๊ทธ๋๋จธ์ค
๋ฒ ์คํธ ์จ๋ฒ

๋ด ๋ต์

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

#### ์ค๋์ ๋ด์ฉ ์ ๋ฆฌ

๋ฐ๋ธ์ฝ์ค Day4

๋งค ์๊ฐ ์ฑ์ฅํ๋ ๊ฐ๋ฐ์๊ฐ ๋๋ ค๊ณ  ๋ธ๋ ฅํ๊ณ  ์์ต๋๋ค.

#### 4๊ฐ์ ๋๊ธ

2022๋ 3์ 24์ผ

์์งํ ์ฒ์์ ์ ์ด์ ๋๋ฉด ๊ด์ฐฎ๊ฒ ํ์๋ค <<< ๊ณต๊ฐํ๊ณ  ๊ฐ๋๋ค ๐๐

1๊ฐ์ ๋ต๊ธ
2022๋ 3์ 24์ผ

์ง์ง ๊ทธ ๋ฌธ์  ํ๊ณ  ์ค์ค๋ก์ ๋ํ ๋ฐ์ฑ์ด ๋ค์์ด์
์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์๋ฐ์คํฌ๋ฆฝํธ ๋ต๊ฒ ์ฐ๋๊ฑด ์ ๋ฐ๊ฑฐ๊ตฌ๋.. ์ถ์
์ธ์ด๋ฅผ ์ธ ๋๋, ์ธ์ด์ ๋ณธ์ง์ ํ์ํ๊ณ  ํน์ฑ์ ๋ง๊ฒ ์ฌ์ฉํ๋ผ๋ ๋ง์ ๋ง์ด ๋ค์๋๋ฐ,
์ด๋์ ๊ณ ์๋ค์ ์ฝ๋๋ฅผ ๋ง์ด ๋ด์ผ๋๊ตฌ๋- ์ถ์๋ ์๊ฐ์ด์๋ค์...

1๊ฐ์ ๋ต๊ธ