diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..722558e --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,8 @@ +## ๐Ÿ“š ๋ฌธ์ œ +- ๋ฌธ์ œ 1 +- ๋ฌธ์ œ 2 +- ๋ฌธ์ œ 3 +- ๋ฌธ์ œ 4 + +## ๐Ÿ“‹ ํ•˜๊ณ  ์‹ถ์€ ๋ง +- ํ•˜๊ณ  ์‹ถ์€ ๋ง 1 \ No newline at end of file diff --git "a/src/Do-ho/dataStructor/pgs_\354\234\204\354\236\245/pgs_\354\234\204\354\236\245.js" "b/src/Do-ho/dataStructor/pgs_\354\234\204\354\236\245/pgs_\354\234\204\354\236\245.js" new file mode 100644 index 0000000..c8b00b6 --- /dev/null +++ "b/src/Do-ho/dataStructor/pgs_\354\234\204\354\236\245/pgs_\354\234\204\354\236\245.js" @@ -0,0 +1,8 @@ +const solution = clothes => { + const clothesObject = clothes.reduce((acc, [name, kind]) => { + acc.hasOwnProperty(kind)? acc[kind] += 1 : acc[kind] = 1; + return acc; + }, new Object()); + + return Object.values(clothesObject).reduce((acc, val) => acc *= val + 1, 1) - 1; +} \ No newline at end of file diff --git "a/src/Do-ho/dfsbfs/pgs_\353\204\244\355\212\270\354\233\214\355\201\254/pgs_\353\204\244\355\212\270\354\233\214\355\201\254.js" "b/src/Do-ho/dfsbfs/pgs_\353\204\244\355\212\270\354\233\214\355\201\254/pgs_\353\204\244\355\212\270\354\233\214\355\201\254.js" new file mode 100644 index 0000000..9a04453 --- /dev/null +++ "b/src/Do-ho/dfsbfs/pgs_\353\204\244\355\212\270\354\233\214\355\201\254/pgs_\353\204\244\355\212\270\354\233\214\355\201\254.js" @@ -0,0 +1,26 @@ +const solution = (n, computers) => { + let answer = 0; + let visited = new Array(n).fill(false); + let queue = []; + const loopArray = [... new Array(n).keys()]; + const isEmpty = () => queue.length !== 0; + + const dfs = () => { + const targetIdx = queue.pop(); + const target = computers[targetIdx] + loopArray.forEach((idx) => { + if(visited[idx] || target[idx] === 0) return; + queue.push(idx); + visited[idx] = true; + }); + } + + loopArray.forEach((computerID) => { + if(visited[computerID]) return; + queue.push(computerID); + while(isEmpty()) dfs(); + answer++; + }) + + return answer; +} \ No newline at end of file diff --git "a/src/Do-ho/implementation/pgs_\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260/pgs_\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260.js" "b/src/Do-ho/implementation/pgs_\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260/pgs_\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260.js" new file mode 100644 index 0000000..3383aab --- /dev/null +++ "b/src/Do-ho/implementation/pgs_\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260/pgs_\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260.js" @@ -0,0 +1,21 @@ +const solution = (n, words) => { + let [people, order] = [0, 0]; + let prevText = null; + let wordSet = []; + const isNotPrevTextNull = () => prevText !== null; + const isNotMatchWord = (word) => prevText[prevText.length - 1] !== word[0]; + const isContainWordSet = (word) => wordSet.includes(word); + + for (let i=0; i { + let answer = 0; + const network = Array.from({ length: n }, () => 0); + + for (let i = 0; i < n; i += 1) { + if (network[i] !== 0) continue; + answer += 1; + network[i] = answer; + + const queue = [i]; + while (queue.length > 0) { + const visit = queue.pop(); + computers[visit].forEach((computer, index) => { + if (computer && network[index] === 0) { + queue.push(index); + network[index] = answer; + } + }); + } + } + + return answer; +}; diff --git a/src/do02reen24/Review/week15.md b/src/do02reen24/Review/week15.md new file mode 100644 index 0000000..af356d6 --- /dev/null +++ b/src/do02reen24/Review/week15.md @@ -0,0 +1,16 @@ +# :fire: week15 + +## :ballot_box_with_check: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์œ„์žฅ(42578) + +- ์กฐํ•ฉ์˜ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•˜์—ฌ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค. +- ํ•ด๋‹น ์˜ท์„ ์•„์˜ˆ ๊ณ ๋ฅด์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์œผ๋ฏ€๋กœ ๊ฐ ์•„์ดํ…œ ๊ธธ์ด์— + 1 ์„ ํ•ด์ฃผ์—ˆ๋‹ค. +- ๋ชจ๋“  ์˜ท์„ ์•ˆ๊ณ ๋ฅด๋Š” ๊ฒฝ์šฐ๋Š” ์ œ์™ธํ•ด์•ผํ•˜๋ฏ€๋กœ ์ •๋‹ต์—์„œ - 1 ์„ ํ•ด์ฃผ์—ˆ๋‹ค. + +## :ballot_box_with_check: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์˜์–ด ๋๋ง์ž‡๊ธฐ(12981) + +- ๊ฐ ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ๋‹ค. + +## :ballot_box_with_check: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋„คํŠธ์›Œํฌ(43162) + +- BFS์˜ ๊ฐœ๋…์„ ํ™œ์šฉํ•˜์—ฌ ํ’€์—ˆ๋‹ค. +- ๋ฐฑ์ค€ 2644๋ฒˆ ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ ๊ฒƒ ๊ฐ™๋‹ค. diff --git a/src/do02reen24/etc/programmers_12981.js b/src/do02reen24/etc/programmers_12981.js new file mode 100644 index 0000000..1411e33 --- /dev/null +++ b/src/do02reen24/etc/programmers_12981.js @@ -0,0 +1,27 @@ +const failUser = (n, index) => { + let person = index % n; + let order = Math.floor(index / n) + 1; + if (person === 0) { + person = n; + order -= 1; + } + return [person, order]; +}; + +const getLastChar = (word) => word.charAt(word.length - 1); + +const solution = (n, words) => { + const dictionary = {}; + let lastChar = words[0][0]; + for (let index = 0; index < words.length; index += 1) { + const word = words[index]; + if (lastChar === word[0] && dictionary[word] === undefined) { + dictionary[word] = true; + lastChar = getLastChar(word); + continue; + } + return failUser(n, index + 1); + } + + return [0, 0]; +}; diff --git a/src/do02reen24/etc/programmers_42578.js b/src/do02reen24/etc/programmers_42578.js new file mode 100644 index 0000000..39d57b9 --- /dev/null +++ b/src/do02reen24/etc/programmers_42578.js @@ -0,0 +1,19 @@ +const getCombinationNumber = (object) => { + let combination = 1; + for (const key in object) { + const itemLength = object[key].length + 1; + combination *= itemLength; + } + return combination; +}; + +const solution = (clothes) => { + const closet = {}; + clothes.forEach(([cloth, clothType]) => { + closet[clothType] + ? closet[clothType].push(cloth) + : (closet[clothType] = [cloth]); + }); + + return getCombinationNumber(closet) - 1; +}; diff --git "a/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/1_BinaryGap.js" "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/1_BinaryGap.js" new file mode 100644 index 0000000..09f2236 --- /dev/null +++ "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/1_BinaryGap.js" @@ -0,0 +1,16 @@ +const solution = (N) => { + let maxLength = 0; + const bin = N.toString(2).split(''); + + bin.reduce((length, word) => { + if (word === '1') { + if (maxLength < length) maxLength = length; + return 0; + } + return length + 1; + }, 0); + + return maxLength; +}; + +console.log(solution(32)); diff --git "a/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/2_CyclicRotation.js" "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/2_CyclicRotation.js" new file mode 100644 index 0000000..d0580a2 --- /dev/null +++ "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/2_CyclicRotation.js" @@ -0,0 +1,9 @@ +const solution = (A, K) => { + const length = A.length; + const k = K % length; + const front = A.slice(length - k, length); + const end = A.slice(0, length - k); + return front.concat(end); +}; + +console.log(solution([3, 8, 9, 7, 6], 3)); diff --git "a/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/2_OddOccurrencesInArray.js" "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/2_OddOccurrencesInArray.js" new file mode 100644 index 0000000..873c722 --- /dev/null +++ "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/2_OddOccurrencesInArray.js" @@ -0,0 +1,11 @@ +const solution = (A) => { + const count = A.reduce((arr, num) => { + if (arr[num] === undefined) arr[num] = 1; + else arr[num] += 1; + return arr; + }, {}); + for (const num in count) { + if (count[num] % 2 !== 0) return num; + } + return -1; +}; diff --git "a/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_FrogJmp.js" "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_FrogJmp.js" new file mode 100644 index 0000000..dc00aa1 --- /dev/null +++ "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_FrogJmp.js" @@ -0,0 +1,3 @@ +const solution = (X, Y, D) => { + return Math.ceil((Y - X) / D); +}; diff --git "a/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_PermMissingElem.js" "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_PermMissingElem.js" new file mode 100644 index 0000000..9833357 --- /dev/null +++ "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_PermMissingElem.js" @@ -0,0 +1,12 @@ +const solution = (A) => { + const N = A.length + 1; + const isNum = {}; + A.forEach((a) => { + isNum[a] = true; + }); + for (let n = 1; n < N + 1; n += 1) { + if (isNum[n] === true) continue; + return n; + } + return -1; +}; diff --git "a/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_TapeEquilibrium.js" "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_TapeEquilibrium.js" new file mode 100644 index 0000000..3fc11e9 --- /dev/null +++ "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_TapeEquilibrium.js" @@ -0,0 +1,13 @@ +const solution = (A) => { + let answer; + let frontSum = 0; + let backSum = A.reduce((a, b) => a + b, 0); + for (let i = 0; i < A.length - 1; i += 1) { + frontSum += A[i]; + backSum -= A[i]; + const absSub = Math.abs(frontSum - backSum); + if (absSub < answer || answer === undefined) answer = absSub; + } + + return answer; +}; diff --git "a/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/7_StoneWall.js" "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/7_StoneWall.js" new file mode 100644 index 0000000..67dd48d --- /dev/null +++ "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/7_StoneWall.js" @@ -0,0 +1,20 @@ +const solution = (H) => { + const queue = [H]; + let block = 0; + while (queue.length > 0) { + const h = queue.pop(); + if (h.length === 0) continue; + const min = Math.min.apply(null, h); + const remain = h.reduce((arr, cur) => { + if (cur === min) { + queue.push(arr); + return []; + } + arr.push(cur); + return arr; + }, []); + queue.push(remain); + block += 1; + } + return block; +}; diff --git "a/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/score.md" "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/score.md" new file mode 100644 index 0000000..2debed9 --- /dev/null +++ "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/score.md" @@ -0,0 +1,21 @@ +# ๐Ÿ“‹ Codility ์ ์ˆ˜ ๊ธฐ๋ก + +| Lesson | ๋ฌธ์ œ | ์ ์ˆ˜ | ๋น„๊ณ  | ์ตœ์ข… ์ œ์ถœ ๋‚ ์งœ | +| :-----------------: | :-------------------: | :--: | :-------------------: | :------------: | +| 1-Iterations | BinaryGap | 100 | - | 2021-01-05 | +| 2-Arrays | CyclicRotation | 100 | - | 2021-01-05 | +| 2-Arrays | OddOccurrencesInArray | 100 | - | 2021-01-05 | +| 3-Time Complexity | FrogJmp | 100 | - | 2021-01-06 | +| 3-Time Complexity | PermMissingElem | 100 | - | 2021-01-06 | +| 3-Time Complexity | TapeEquilibrium | 100 | - | 2021-01-06 | +| 7-Stacks and Queues | StoneWall | 85 | ์ •ํ™•๋„ 100%, ์„ฑ๋Šฅ 77% | 2021-01-05 | + +# ๐Ÿ“š Perfect Lesson + +> ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ 100%๋กœ ๋๋‚ธ Lesson + +* Lesson1 - Iterations + +* Lesson2 - Arrays +* Lesson3 - Time Complexity + diff --git a/src/grap3fruit/js/programmers/12981.js b/src/grap3fruit/js/programmers/12981.js new file mode 100644 index 0000000..57214da --- /dev/null +++ b/src/grap3fruit/js/programmers/12981.js @@ -0,0 +1,38 @@ +const getResult = (idx, n) => { + return [(idx % n) + 1, Math.ceil((idx + 1) / n)]; +}; + +const check = (lastWord, n, word, idx, visited) => { + if (visited.get(word)) { + return getResult(idx, n); + } + if (idx > 0) { + if (lastWord[lastWord.length - 1] !== word[0]) { + return getResult(idx, n); + } + } + return null; +}; + +function solution(n, words) { + let answer = null; + const visited = new Map(); + + words.forEach((word, idx) => { + if (!answer) { + answer = check(words[idx - 1], n, word, idx, visited); + visited.set(word, true); + } + }); + if (!answer) { + answer = [0, 0]; + } + return answer; +} + +const n = 2; +const words = ['hello', 'two']; +// const words = ['hello', 'one', 'even', 'never', 'now', 'world', 'draw']; +// const words = ['tank', 'kick', 'know', 'wheel', 'land', 'dream', 'mother', 'robot', 'tank']; + +console.log(solution(n, words)); diff --git a/src/grap3fruit/js/programmers/12981.md b/src/grap3fruit/js/programmers/12981.md new file mode 100644 index 0000000..a7d413a --- /dev/null +++ b/src/grap3fruit/js/programmers/12981.md @@ -0,0 +1,11 @@ +# 12981 ์˜์–ด ๋๋ง์ž‡๊ธฐ - ์„ฑ๊ณต + +## ์•„์ด๋””์–ด ๊ตฌํ˜„ + +๋‹จ์ˆœ ๊ตฌํ˜„ ๋ฌธ์ œ + +words๋ฅผ ๋Œ๋ฉด์„œ ์กฐ๊ฑด์— ๋งž๋Š”๊ฒƒ ๋ฆฌํ„ด ํ•ด์ฃผ๋ฉด ๋จ. + +[๋ฒˆํ˜ธ, ์ฐจ๋ก€]๋ฅผ ์ž˜ ์ถœ๋ ฅํ•˜๋Š”๊ฒŒ ๊นŒ๋‹ค๋กœ์› ์Œ. + +์ฐจ๋ก€๋Š” ํ•ญ์ƒ ์˜ฌ๋ฆผ์œผ๋กœ ํ•ด์ฃผ๋ฉด ๋งž๊ฒŒ ๋œ๋‹ค. diff --git a/src/grap3fruit/js/programmers/42578.js b/src/grap3fruit/js/programmers/42578.js new file mode 100644 index 0000000..3ec1378 --- /dev/null +++ b/src/grap3fruit/js/programmers/42578.js @@ -0,0 +1,26 @@ +function solution(clothes) { + let answer = 1; + const data = {}; + + clothes.forEach((cloth) => { + if (!data[cloth[1]]) { + return (data[cloth[1]] = [cloth[0]]); + } + data[cloth[1]].push(cloth[0]); + }); + + for (const el in data) { + answer *= data[el].length + 1; + } + answer -= 1; + + return answer; +} + +const clothes = [ + ['yellow_hat', 'headgear'], + ['blue_sunglasses', 'eyewear'], + ['green_turban', 'headgear'], +]; + +console.log(solution(clothes)); diff --git a/src/grap3fruit/js/programmers/42578.md b/src/grap3fruit/js/programmers/42578.md new file mode 100644 index 0000000..18702d0 --- /dev/null +++ b/src/grap3fruit/js/programmers/42578.md @@ -0,0 +1,9 @@ +# 42578 ์œ„์žฅ - ์„ฑ๊ณต + +## ์•„์ด๋””์–ด ๊ตฌํ˜„ + +์ด ๋ฌธ์ œ๊ฐ€ ์™œ ํ•ด์‹œ์ธ๊ฐ€? ๋”•์…”๋„ˆ๋ฆฌ ์•„๋‹Œ๊ฐ€? ํ–ˆ๋”๋‹ˆ + +ํ•ด์‹œ๋งต === ๋”•์…”๋„ˆ๋ฆฌ ๋„ค์š”. ๊ฐ™์€ ์ž๋ฃŒ๊ตฌ์กฐ์ธ๋ฐ ์—ฌ๋Ÿฌ๊ฐ€์ง€๋กœ ๋ถˆ๋ฆฌ๋Š”. + +[ํ‚ค๊ธฐ๋ฐ˜์˜ ์ปฌ๋ ‰์…˜](https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Keyed_collections#Maps) ์ฐธ๊ณ ํ•ด์„œ, map ์จ์„œ๋„ ๊ตฌํ˜„ํ•ด๋ดค์Šต๋‹ˆ๋‹ค diff --git a/src/grap3fruit/js/programmers/42578_2.js b/src/grap3fruit/js/programmers/42578_2.js new file mode 100644 index 0000000..f7b369c --- /dev/null +++ b/src/grap3fruit/js/programmers/42578_2.js @@ -0,0 +1,29 @@ +function solution(clothes) { + let answer = 1; + console.log(clothes); + + const map = new Map(); + + clothes.forEach((cloth) => { + if (!map.get(cloth[1])) { + return map.set(cloth[1], [cloth[0]]); + } + + map.get(cloth[1]).push(cloth[0]); + }); + + for (let key of map.keys()) { + answer *= map.get(key).length + 1; + } + answer -= 1; + + return answer; +} + +const clothes = [ + ['yellow_hat', 'headgear'], + ['blue_sunglasses', 'eyewear'], + ['green_turban', 'headgear'], +]; + +console.log(solution(clothes)); diff --git a/src/grap3fruit/js/programmers/43162.js b/src/grap3fruit/js/programmers/43162.js new file mode 100644 index 0000000..0affc25 --- /dev/null +++ b/src/grap3fruit/js/programmers/43162.js @@ -0,0 +1,49 @@ +const bfs = (computers, root) => { + const visited = [root]; + let q = [computers[root]]; + + while (q.length > 0) { + let item = q.shift(); + + item.forEach((el, idx) => { + if (el === 1 && !visited.includes(idx)) { + q.push(computers[idx]); + visited.push(idx); + } + }); + } + + return visited; +}; + +function solution(n, computers) { + let answer = 0; + const visited = []; + + computers.forEach((_, idx) => { + const visitFlag = visited.some((el) => el.includes(idx)); + + if (!visitFlag) { + const newVisited = bfs(computers, idx); + visited.push(newVisited); + } + }); + + console.log(visited.length); + answer = visited.length; + return answer; +} + +const n = 3; +const computers = [ + [1, 1, 0], + [1, 1, 0], + [0, 0, 1], +]; +// const computers = [ +// [1, 1, 0], +// [1, 1, 1], +// [0, 1, 1], +// ]; + +solution(n, computers); diff --git a/src/grap3fruit/js/programmers/43162.md b/src/grap3fruit/js/programmers/43162.md new file mode 100644 index 0000000..107a9be --- /dev/null +++ b/src/grap3fruit/js/programmers/43162.md @@ -0,0 +1,11 @@ +# 43162 ๋„คํŠธ์›Œํฌ - ์„ฑ๊ณต + +## BFS + +bfs์—์„œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•  ํ•„์š”๋Š” ์—†๊ณ , ์ง€๋‚˜๊ฐ„ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ visited์— ๋‹ด์•„์„œ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์ด๊ฑธ ํ•˜๋‚˜์˜ ๋„คํŠธ์›Œํฌ๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. + +๊ฐ๊ฐ์˜ ์ปดํ“จํ„ฐ๊ฐ€ root์ผ๋•Œ ๋„คํŠธ์›Œํฌ๋ฅผ ๊ตฌํ•˜๊ณ  ๊ฒน์น˜์ง€ ์•Š๋Š” ๋„คํŠธ์›Œํฌ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค. + +๊ฒน์น˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ์œ„ํ•ด bfs๋กœ root๋ฅผ ๋„ฃ๊ธฐ ์ „, network๋ฅผ ๋ชจ๋‘ ์ฒดํฌํ•ด์„œ ๊ฒน์น˜๋Š” root๊ฐ€ ์žˆ์œผ๋ฉด ๊ฐ™์€ ๋„คํŠธ์›Œํฌ๊ฐ€ ์ด๋ฏธ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ pass ํ–ˆ๋‹ค. + +์œ„ ์กฐ๊ฑด์— ๋”ฐ๋ฅธ ๋ชจ๋“  network๋ฅผ ๊ตฌํ•œ ํ›„ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋ฉด ๋. diff --git "a/src/kyu9341/programmers_\353\204\244\355\212\270\354\233\214\355\201\254/index.js" "b/src/kyu9341/programmers_\353\204\244\355\212\270\354\233\214\355\201\254/index.js" new file mode 100644 index 0000000..b095ce3 --- /dev/null +++ "b/src/kyu9341/programmers_\353\204\244\355\212\270\354\233\214\355\201\254/index.js" @@ -0,0 +1,50 @@ +const initArray = (size, val = null) => Array.from({ length: size }, () => val); +const initArrayInArray = size => + Array.from({ length: size }, () => new Array()); + +const solution = (n, computers) => { + const network = initArrayInArray(n); + const check = initArray(n, false); + + computers.forEach((row, rowIdx) => { + row.forEach((computer, colIdx) => { + if (rowIdx !== colIdx && computer) network[rowIdx].push(colIdx); + }); + }); + + const dfs = (node, network) => { + check[node] = true; + + network[node].forEach((_, computer) => { + const next = network[node][computer]; + if (!check[next]) dfs(next, network); + }); + }; + + const answer = network.reduce((acc, _, computer) => { + if (!check[computer]) { + dfs(computer, network); + return acc + 1; + } + return acc; + }, 0); + + return answer; +}; + +(() => { + const inputs = [ + [ + [1, 1, 0], + [1, 1, 0], + [0, 0, 1], + ], + [ + [1, 1, 0], + [1, 1, 1], + [0, 1, 1], + ], + ]; + + inputs.forEach(input => console.log(solution(input.length, input))); +})(); diff --git "a/src/kyu9341/programmers_\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260/index.js" "b/src/kyu9341/programmers_\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260/index.js" new file mode 100644 index 0000000..b44f5f0 --- /dev/null +++ "b/src/kyu9341/programmers_\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260/index.js" @@ -0,0 +1,63 @@ +const solution = (n, words) => { + const set = new Set(); + let prevWrod = ''; + + for (const [idx, word] of words.entries()) { + const beNotEqual = + idx && prevWrod.charAt(prevWrod.length - 1) !== word.charAt(0); + + if (set.has(word) || beNotEqual) { + const num = (idx + 1) % n || n; + const count = Math.floor((idx + 1) / n) + Number(num !== n); + + return [num, count]; + } + + set.add(word); + prevWrod = word; + } + + return [0, 0]; +}; + +(() => { + const inputs = [ + { + n: 3, + words: [ + 'tank', + 'kick', + 'know', + 'wheel', + 'land', + 'dream', + 'mother', + 'robot', + 'tank', + ], + }, + { + n: 5, + words: [ + 'hello', + 'observe', + 'effect', + 'take', + 'either', + 'recognize', + 'encourage', + 'ensure', + 'establish', + 'hang', + 'gather', + 'refer', + 'reference', + 'estimate', + 'executive', + ], + }, + { n: 2, words: ['hello', 'one', 'even', 'never', 'now', 'world', 'draw'] }, + ]; + + inputs.forEach(input => console.log(solution(input.n, input.words))); +})(); diff --git "a/src/kyu9341/programmers_\354\234\204\354\236\245/index.js" "b/src/kyu9341/programmers_\354\234\204\354\236\245/index.js" new file mode 100644 index 0000000..c8a72df --- /dev/null +++ "b/src/kyu9341/programmers_\354\234\204\354\236\245/index.js" @@ -0,0 +1,23 @@ +const solution = clothes => { + const clothesMap = clothes.reduce((map, cur) => { + const countOfCurrentType = map.get(cur[1]); + + return map.set(cur[1], countOfCurrentType ? countOfCurrentType + 1 : 1); + }, new Map()); + + const count = + [...clothesMap.values()].reduce((acc, cur) => acc * (cur + 1), 1) - 1; + return count; +}; + +(() => { + const inputs = [ + [ + ['yellow_hat', 'headgear'], + ['blue_sunglasses', 'eyewear'], + ['green_turban', 'headgear'], + ], + ]; + + inputs.forEach(input => console.log(solution(input))); +})();