diff --git a/README.md b/README.md index 09b7a87..0509c75 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ ## ๐ **๋์์ด ๋์ จ๋ค๋ฉด** ์ค๋ฅธ์ชฝ ์๋จ โ ์ โญ๏ธ **Star๋ฅผ ํด๋ฆญ**ํด ์ด ํ๋ก์ ํธ๋ฅผ ์์ํด์ฃผ์ธ์! ## ํ์ด ํํฉ + ํ์ด ์๋ฃ๋ก ํ์๋ Level์ ํด๋์ ํฌํจ๋์ง ์์ ๋ต์์ด ์๋ค๋ฉด - [Issues](https://github.com/codeisneverodd/programmers-coding-test/issues) ์ ์ ๋ณดํด์ฃผ์ธ์. `New issue`๋ฅผ ๋๋ฅด๊ณ ์ ๋ชฉ๊ณผ ๋ด์ฉ์ @@ -25,9 +26,11 @@ - ํ์ด ๋ฌธ์ ์: 43๋ฌธ์ (2022.03.28) - ํ์ด ์๋ฃ ์์ ์์ : 2022๋ 4์ ์ค -### Level 3 +### Level 3 ๐จ๐ปโ๐ป(ํ์ด ์ค..) -ํ์ด ์๋ฃ ์์ ์์ : 2022๋ 8์ ์ค +- ์ ์ฒด ๋ฌธ์ ์: 52๋ฌธ์ +- ํ์ด ๋ฌธ์ ์: 5๋ฌธ์ (2022.03.24) +- ํ์ด ์๋ฃ ์์ ์์ : 2022๋ 8์ ์ค ### Level 4 diff --git "a/level-2/\355\224\204\353\246\260\355\204\260.js" "b/level-2/\355\224\204\353\246\260\355\204\260.js" index 95adcbe..e8c30d5 100644 --- "a/level-2/\355\224\204\353\246\260\355\204\260.js" +++ "b/level-2/\355\224\204\353\246\260\355\204\260.js" @@ -19,7 +19,7 @@ function solution(priorities, location) { return answer; } -//์ ๋ต 2 - codisneverodd +//์ ๋ต 2 - codeisneverodd function solution(priorities, location) { var answer = 0; let documents = priorities.map((priority, documentLocation) => [documentLocation, priority]) @@ -46,9 +46,9 @@ function solution(priorities, location) { //์ ๋ต 3 - jaewon1676 function solution(priorities, location) { var answer = 0; - while(true){ + while (true) { - if (priorities[0] < Math.max(...priorities)){ + if (priorities[0] < Math.max(...priorities)) { if (location - 1 < 0) location = priorities.length priorities.push(priorities.shift()) location--; @@ -64,4 +64,53 @@ function solution(priorities, location) { } return answer +} + +//์ ๋ต 4 - codeisneverodd +//shift๋ฅผ ์ฌ์ฉํ์ง ์๊ณ queue๋ฅผ ๊ตฌํํ ํ์ด๋ฅผ ์ถ๊ฐํฉ๋๋ค. +function solution(priorities, location) { + let answer = 0; + const printer = new Queue; + priorities.forEach((priority, index) => { + printer.enqueue([priority, index]) + }) + while (printer.size() > 0) { + const check = printer.dequeue() + const countHigherPriority = printer.queue.filter(x => x[0] > check[0]).length + if (countHigherPriority > 0) { + printer.enqueue(check) + } else { + answer += 1 + if (check[1] === location) break + } + + } + return answer; +} + +class Queue { + constructor() { + this.queue = [] + this.front = 0 + this.rear = 0 + } + + enqueue(value) { + this.queue[this.rear++] = value + } + + dequeue() { + const value = this.queue[this.front] + delete this.queue[this.front] + this.front += 1 + return value + } + + peek() { + return this.queue(this.front) + } + + size() { + return this.rear - this.front + } } \ No newline at end of file diff --git "a/level-2/\355\233\204\353\263\264\355\202\244.js" "b/level-2/\355\233\204\353\263\264\355\202\244.js" new file mode 100644 index 0000000..b0d86e8 --- /dev/null +++ "b/level-2/\355\233\204\353\263\264\355\202\244.js" @@ -0,0 +1,50 @@ +//https://github.com/codeisneverodd/programmers-coding-test +//์๋ฒฝํ ์ ๋ต์ด ์๋๋๋ค. +//์ ๋ต 1 - codeisneverodd +function solution(relation) { + //1. ๊ฐ๋ฅํ ์กฐํฉ์ 1๊ฐ~Attribute๊ฐ์ ๋งํผ ์ฐพ๋๋ค. + //2. ํด๋น ๊ฐ์์ ์กฐํฉ์ด ํค๊ฐ ๋ ์ ์๋์ง ๊ฒ์ฌํ๊ณ , ๊ฐ๋ฅํ๋ฉด ํ๋ณดํค์ ์ถ๊ฐํ๋ค. + //3. ๋จ ์ถ๊ฐํ๋ ค๊ณ ํ ๋, ํ๋ณดํค์ ์๋ ๊ฐ์ด ์์ ์ ๋ถ๋ถ ์งํฉ์ด ๋ ์ ์์ผ๋ฉด ์ถ๊ฐํ์ง ์๋๋ค. + const keys = [] + const totalAttrCount = relation[0].length + const indexList = Array.from(Array(totalAttrCount), (x, index) => index) // [0,1,2,3 ... totalAttrCount-1] + + //Fn for 2. ํด๋น ์กฐํฉ์ผ๋ก ๊ฐ row์ attribute๋ฅผ ๋ชจ์์ ๋ ์ค๋ณต์ด ์๋์ง๋ฅผ ๋ฐํํ๋ ํจ์ + const isUnique = (relation, attrIndexComb) => { + let result = Array.from(Array(relation.length), x => '') + for (const attrIndex of attrIndexComb) { + relation.forEach((row, rowIndex) => result[rowIndex] += row[attrIndex]) //Set๋ฅผ ์ด์ฉํด ์ค๋ณต ๊ฒ์ฌ๋ฅผ ํ๊ธฐ ์ํด result์ string์ผ๋ก ๋ฃ์. + } + return result.length === [...new Set(result)].length + } + + //Fn for 3. keys์ ํ์ฌ ๊ตฌํ ๊ฒ์ฌํ ์กฐํฉ์ ๋ถ๋ถ์งํฉ์ด ์กด์ฌํ๋์ง ๋ฐํ, ๋จ keys์ ๋ค์ด์๋ ๊ฐ ์กฐํฉ์ ํฌ๊ธฐ๋ ํ์ฌ ๊ฒ์ฌํ ์กฐํฉ์ ํฌ๊ธฐ๋ณด๋ค ์๋ค. + const isMinimal = (attrComb) => { + for (const key of keys) if (key.every(attr => attrComb.includes(attr))) return false + return true + } + + //๊ฐ๋ฅํ ๋ชจ๋ ์กฐํฉ์ ๊ฒ์ฌ + for (let attrCount = 1; attrCount <= totalAttrCount; attrCount++) { + const combinations = getCombinations(indexList, attrCount) + for (const attrComb of combinations) { + if (isMinimal(attrComb) && isUnique(relation, attrComb)) keys.push(attrComb) + } + } + + return keys.length +} + +//Fn for 1. ์กฐํฉ์ ๋ฐํํ๋ ํจ์ +const getCombinations = (array, selectNumber) => { + const result = []; + if (selectNumber === 1) { + return array.map((element) => [element]); + } + array.forEach((fixed, index, origin) => { + const restCombinations = getCombinations(origin.slice(index + 1), selectNumber - 1); + const attached = restCombinations.map((restCombination) => [fixed, ...restCombination]); + result.push(...attached); + }); + return result; +} diff --git "a/level-3/\352\260\200\354\236\245-\353\250\274-\353\205\270\353\223\234.js" "b/level-3/\352\260\200\354\236\245-\353\250\274-\353\205\270\353\223\234.js" new file mode 100644 index 0000000..42f4285 --- /dev/null +++ "b/level-3/\352\260\200\354\236\245-\353\250\274-\353\205\270\353\223\234.js" @@ -0,0 +1,23 @@ +//https://github.com/codeisneverodd/programmers-coding-test +//์๋ฒฝํ ์ ๋ต์ด ์๋๋๋ค. +//์ ๋ต 1 - codeisneverodd +function solution(n, edge) { + const graph = Array.from(Array(n + 1), () => []) + for (const [src, dest] of edge) { + graph[src].push(dest) + graph[dest].push(src) + } + const distance = Array(n + 1).fill(0) + distance[1] = 1 + const toBeSearched = [1] + while (toBeSearched.length > 0) { + const src = toBeSearched.shift() + for (const dest of graph[src]) { + if (distance[dest] === 0) { + distance[dest] = distance[src] + 1 + toBeSearched.push(dest) + } + } + } + return distance.filter(x => x === Math.max(...distance)) +} \ No newline at end of file diff --git "a/level-3/\353\204\244\355\212\270\354\233\214\355\201\254.js" "b/level-3/\353\204\244\355\212\270\354\233\214\355\201\254.js" new file mode 100644 index 0000000..a4fa59d --- /dev/null +++ "b/level-3/\353\204\244\355\212\270\354\233\214\355\201\254.js" @@ -0,0 +1,30 @@ +//https://github.com/codeisneverodd/programmers-coding-test +//์๋ฒฝํ ์ ๋ต์ด ์๋๋๋ค. +//์ ๋ต 1 - codeisneverodd +function solution(n, computers) { + let answer = 0 + const visited = new Array(n).fill(false) + const newNetwork = (startComputer) => { + //์๋ก์ด ๋คํธ์ํฌ๋ฅผ ๋ง๋ค ์์ ์ปดํจํฐ๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๋๋ค. + const toBeVisited = [startComputer] + while (toBeVisited.length > 0) { + //์์ ์ปดํจํฐ๋ก๋ถํฐ ๋ฐฉ๋ฌธ ๊ฐ๋ฅํ ์ปดํจํฐ๋ฅผ ๋ชจ๋ ๋ฐฉ๋ฌธํ๋ฉฐ ํด๋น ์ปดํจํฐ์ visited๋ฅผ true๋ก ๋ฐ๊พผ๋ค + const currentComputer = toBeVisited.pop() + visited[currentComputer] = true + for (let nextComputer = 0; nextComputer < n; nextComputer++) { + if (!visited[nextComputer] && computers[currentComputer][nextComputer]) { + toBeVisited.push(nextComputer) + } + } + } + } + + for (let startComputer = 0; startComputer < n; startComputer++) { + if (!visited[startComputer]) { + newNetwork(startComputer) + //์๋ก์ด ๋คํธ์ํฌ๋ฅผ ์์ฑํ ๋๋ง๋ค ์ ๋ต์ 1 ์ฆ๊ฐ์ํจ๋ค. + answer++ + } + } + return answer +} \ No newline at end of file diff --git "a/level-3/\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.js" "b/level-3/\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.js" new file mode 100644 index 0000000..5b45d5b --- /dev/null +++ "b/level-3/\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.js" @@ -0,0 +1,48 @@ +//https://github.com/codeisneverodd/programmers-coding-test +//์๋ฒฝํ ์ ๋ต์ด ์๋๋๋ค. +//์ ๋ต 1 - codeisneverodd +function solution(genres, plays) { + var answer = []; + const songs = [] + const genreSumHash = {} + const genreSumArr = [] + + //๊ณ ์ ๋ฒํธ, ์ฅ๋ฅด, ํ๋ ์ด์๋ฅผ ๋ด์ songs + genres.forEach((genre, id) => { + songs.push({id: id, genre: genre, play: plays[id]}) + genreSumHash[genre] = genreSumHash[genre] === undefined ? plays[id] : genreSumHash[genre] + plays[id] + }) + + //์ฅ๋ฅด๋ณ ํ๋ ์ด์ ํฉ์ผ๋ก ์ ๋ ฌํ๊ธฐ ์ํด ์์ฑํ ๋ฐฐ์ด genreSumArr + for (const genre in genreSumHash) genreSumArr.push([genre, genreSumHash[genre]]) + genreSumArr.sort((a, b) => b[1] - a[1]) + + //๊ฐ ์ฅ๋ฅด์์์ ๊ฐ ๋ ธ๋์ play์๊ฐ ๋์ ์์ผ๋ก ์ ๋ ฌํ๊ณ ์์์๋ถํฐ 2๊ฐ๊น์ง ์ ๋ต์ ๊ณ ์ ๋ฒํธ๋ฅผ push + for (const genre of genreSumArr) { + const sorted = songs.filter(song => song.genre === genre[0]).sort((a, b) => b.play - a.play) + for (let i = 0; i < 2 && i < sorted.length; i++) answer.push(sorted[i].id) + } + return answer; +} + +//์ ๋ต 2 - codeisneverodd +//Map๊ณผ ๊ณ ์ฐจํจ์๋ฅผ ์ ๊ทน์ ์ผ๋ก ์ด์ฉํ ํ์ด +function solution(genres, plays) { + const genreMap = new Map(); // {genre:{totalPlay:number, songs:[{play:number, id:number}, ...]} + genres + .map((genre, id) => [genre, plays[id]]) + .forEach(([genre, play], id) => { + const data = genreMap.get(genre) || {totalPlay: 0, songs: []} + genreMap.set(genre, { + totalPlay: data.totalPlay + play, + songs: [...data.songs, {play: play, id: id}] + .sort((a, b) => b.play - a.play) + .slice(0, 2) + }) + }) + + return [...genreMap.entries()] //entries => [[genre, {totalPlay, songs}], ...] + .sort((a, b) => b[1].totalPlay - a[1].totalPlay) + .flatMap(item => item[1].songs) // [[songs], [songs]] => [songs, songs] + .map(song => song.id) +} \ No newline at end of file diff --git "a/level-3/\354\227\254\355\226\211\352\262\275\353\241\234.js" "b/level-3/\354\227\254\355\226\211\352\262\275\353\241\234.js" new file mode 100644 index 0000000..2317d7b --- /dev/null +++ "b/level-3/\354\227\254\355\226\211\352\262\275\353\241\234.js" @@ -0,0 +1,24 @@ +//https://github.com/codeisneverodd/programmers-coding-test +//์๋ฒฝํ ์ ๋ต์ด ์๋๋๋ค. +//์ ๋ต 1 - codeisneverodd +function solution(tickets) { + const routes = [] //์ต์ข ๊ฐ๋ฅ ๋ฃจํธ๋ค์ ๋ด์ ๋ฐฐ์ด + const makeRoutes = (currentDepart, remainTickets, currentRoute) => { + //ํ์ฌ ์ถ๋ฐ์ง, ๋จ์ ํฐ์ผ๋ค, ํ์ฌ ๊น์ง ๋ง๋ ๋ฃจํธ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ฒฝ๋ก๋ฅผ ๋ง๋ค์ด ๊ฐ๋ ์ฌ๊ท ํจ์ + if (remainTickets.length > 0) { + remainTickets.forEach(([depart, nextDepart], index) => { + if (depart === currentDepart) + //ํ์ฌ ์ถ๋ฐ์ง์ ๊ฐ์ ์ถ๋ฐ์ง๋ฅผ ๊ฐ์ง ํฐ์ผ์ด ์๋ค๋ฉด, ํด๋น ํฐ์ผ์ ์ฌ์ฉํ๊ณ ํด๋น ํฐ์ผ์ ๋์ฐฉ์ง๋ฅผ ๋ค์ ์ถ๋ฐ์ง๋ก ์ง์ + makeRoutes( + nextDepart, + [...remainTickets.slice(0, index), ...remainTickets.slice(index + 1)], + [...currentRoute, currentDepart]) + }) + } else { + //ํฐ์ผ์ ๋ชจ๋ ์ฌ์ฉํ๋ฉด ์ต์ข ๊ฐ๋ฅ ๋ฃจํธ์ ํฌํจ + routes.push([...currentRoute, currentDepart]) + } + } + makeRoutes("ICN", tickets, []) + return routes.sort()[0] +} \ No newline at end of file diff --git "a/level-3/\354\236\205\352\265\255\354\213\254\354\202\254.js" "b/level-3/\354\236\205\352\265\255\354\213\254\354\202\254.js" new file mode 100644 index 0000000..e970c54 --- /dev/null +++ "b/level-3/\354\236\205\352\265\255\354\213\254\354\202\254.js" @@ -0,0 +1,19 @@ +//https://github.com/codeisneverodd/programmers-coding-test +//์๋ฒฝํ ์ ๋ต์ด ์๋๋๋ค. +//์ ๋ต 1 - codeisneverodd +function solution(n, times) { + //์ต์๋ก ๊ฑธ๋ฆด ์ ์๋ ์๊ฐ left, ์ต๋๋ก ๊ฑธ๋ฆด ์ ์๋ ์๊ฐ right + let [left, right] = [1, Math.max(...times) * n] + while (left <= right) { + const mid = Math.floor((left + right) / 2) + const sum = times.reduce((acc, time) => acc + Math.floor(mid / time), 0) + //sum์ mid ์๊ฐ ๋์ ์ฒ๋ฆฌ ํ ์ ์๋ ์ฌ๋์ ์ + if (sum < n) { + left = mid + 1 + } else { + right = mid - 1 + } + } + // left ๊ฐ right๋ฅผ ๋์ด๊ฐ๋ค๋ ๊ฒ์ left๊ฐ n๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์์ ธ์ n๋ช ์ ์์ฉํ ์ ์ต์๊ฐ์ด ๋์๋ค๋ ๊ฒ์ด๋ค. + return left; +} \ No newline at end of file