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