From 3cabe485e5cc8a7740aba4b956091c8289d763a4 Mon Sep 17 00:00:00 2001 From: do02reen24 Date: Sun, 3 Jan 2021 23:13:01 +0900 Subject: [PATCH 01/20] =?UTF-8?q?solve=20:=20programmers=20=EC=9C=84?= =?UTF-8?q?=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/do02reen24/Review/week15.md | 7 +++++++ src/do02reen24/etc/programmers_42578.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/do02reen24/Review/week15.md create mode 100644 src/do02reen24/etc/programmers_42578.js diff --git a/src/do02reen24/Review/week15.md b/src/do02reen24/Review/week15.md new file mode 100644 index 0000000..7b70f0d --- /dev/null +++ b/src/do02reen24/Review/week15.md @@ -0,0 +1,7 @@ +# :fire: week15 + +## :ballot_box_with_check: 프로그래머스 위장(42578) + +- 조합의 개념을 사용하여 풀 수 있었다. +- 해당 옷을 아예 고르지 않는 경우도 있으므로 각 아이템 길이에 + 1 을 해주었다. +- 모든 옷을 안고르는 경우는 제외해야하므로 정답에서 - 1 을 해주었다. 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; +}; From 3dbc38466184f765ee6f04c756573ed0a19bf813 Mon Sep 17 00:00:00 2001 From: do02reen24 Date: Mon, 4 Jan 2021 15:25:04 +0900 Subject: [PATCH 02/20] =?UTF-8?q?solve=20:=20programmers=20=EC=98=81?= =?UTF-8?q?=EC=96=B4=20=EB=81=9D=EB=A7=90=EC=9E=87=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/do02reen24/Review/week15.md | 4 ++++ src/do02reen24/programmers_12981.js | 31 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/do02reen24/programmers_12981.js diff --git a/src/do02reen24/Review/week15.md b/src/do02reen24/Review/week15.md index 7b70f0d..0363baf 100644 --- a/src/do02reen24/Review/week15.md +++ b/src/do02reen24/Review/week15.md @@ -5,3 +5,7 @@ - 조합의 개념을 사용하여 풀 수 있었다. - 해당 옷을 아예 고르지 않는 경우도 있으므로 각 아이템 길이에 + 1 을 해주었다. - 모든 옷을 안고르는 경우는 제외해야하므로 정답에서 - 1 을 해주었다. + +## :ballot_box_with_check: 프로그래머스 영어 끝말잇기(12981) + +- 각 경우를 고려하여 처리해주었다. diff --git a/src/do02reen24/programmers_12981.js b/src/do02reen24/programmers_12981.js new file mode 100644 index 0000000..2b31345 --- /dev/null +++ b/src/do02reen24/programmers_12981.js @@ -0,0 +1,31 @@ +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]; +}; + +console.log( + solution(2, ['hello', 'one', 'even', 'never', 'now', 'world', 'draw']) +); From ed57e46c70f81f2e2d9326203498439ff33e464a Mon Sep 17 00:00:00 2001 From: do02reen24 Date: Tue, 5 Jan 2021 13:50:58 +0900 Subject: [PATCH 03/20] =?UTF-8?q?move=20:=20=ED=8C=8C=EC=9D=BC=20=EC=9C=84?= =?UTF-8?q?=EC=B9=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/do02reen24/{ => etc}/programmers_12981.js | 4 ---- 1 file changed, 4 deletions(-) rename src/do02reen24/{ => etc}/programmers_12981.js (87%) diff --git a/src/do02reen24/programmers_12981.js b/src/do02reen24/etc/programmers_12981.js similarity index 87% rename from src/do02reen24/programmers_12981.js rename to src/do02reen24/etc/programmers_12981.js index 2b31345..1411e33 100644 --- a/src/do02reen24/programmers_12981.js +++ b/src/do02reen24/etc/programmers_12981.js @@ -25,7 +25,3 @@ const solution = (n, words) => { return [0, 0]; }; - -console.log( - solution(2, ['hello', 'one', 'even', 'never', 'now', 'world', 'draw']) -); From ab0554efbee4fd2691d89b68f185074db825b442 Mon Sep 17 00:00:00 2001 From: do02reen24 Date: Tue, 5 Jan 2021 14:18:52 +0900 Subject: [PATCH 04/20] =?UTF-8?q?solve=20:=20programmers=20=EB=84=A4?= =?UTF-8?q?=ED=8A=B8=EC=9B=8C=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/do02reen24/BFS/programmers_43162.js | 23 +++++++++++++++++++++++ src/do02reen24/Review/week15.md | 5 +++++ 2 files changed, 28 insertions(+) create mode 100644 src/do02reen24/BFS/programmers_43162.js diff --git a/src/do02reen24/BFS/programmers_43162.js b/src/do02reen24/BFS/programmers_43162.js new file mode 100644 index 0000000..93110e2 --- /dev/null +++ b/src/do02reen24/BFS/programmers_43162.js @@ -0,0 +1,23 @@ +const solution = (n, computers) => { + 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 index 0363baf..af356d6 100644 --- a/src/do02reen24/Review/week15.md +++ b/src/do02reen24/Review/week15.md @@ -9,3 +9,8 @@ ## :ballot_box_with_check: 프로그래머스 영어 끝말잇기(12981) - 각 경우를 고려하여 처리해주었다. + +## :ballot_box_with_check: 프로그래머스 네트워크(43162) + +- BFS의 개념을 활용하여 풀었다. +- 백준 2644번 문제와 비슷한 것 같다. From 0fee3913d7111e989bb9df8bb98fec61b4a79002 Mon Sep 17 00:00:00 2001 From: do02reen24 Date: Tue, 5 Jan 2021 17:20:40 +0900 Subject: [PATCH 05/20] chore : gitignore update --- src/do02reen24/.gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/do02reen24/.gitignore b/src/do02reen24/.gitignore index 24ab719..57d45e2 100644 --- a/src/do02reen24/.gitignore +++ b/src/do02reen24/.gitignore @@ -1,2 +1,2 @@ -카카오코테 +코딩테스트 .vscode \ No newline at end of file From 9b5433d251f9f0581bb96697c5bf345341bab317 Mon Sep 17 00:00:00 2001 From: do02reen24 Date: Tue, 5 Jan 2021 17:21:37 +0900 Subject: [PATCH 06/20] solve : codility 4 problems - binary gap - cyclic rotation - odd occurrences in array - stone wall --- .../codility/1_BinaryGap.js" | 16 +++++++++++++++ .../codility/2_CyclicRotation.js" | 9 +++++++++ .../codility/2_OddOccurrencesInArray.js" | 11 ++++++++++ .../codility/7_StoneWall.js" | 20 +++++++++++++++++++ .../codility/score.md" | 8 ++++++++ 5 files changed, 64 insertions(+) create mode 100644 "src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/1_BinaryGap.js" create mode 100644 "src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/2_CyclicRotation.js" create mode 100644 "src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/2_OddOccurrencesInArray.js" create mode 100644 "src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/7_StoneWall.js" create mode 100644 "src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/score.md" 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/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..4716041 --- /dev/null +++ "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/score.md" @@ -0,0 +1,8 @@ +# 📋 Codility 점수 기록 + +| Lesson | 문제 | 점수 | 비고 | +| :-----------------: | :-------------------: | :--: | :-------------------: | +| 1-Iterations | BinaryGap | 100 | - | +| 2-Arrays | CyclicRotation | 100 | - | +| 2-Arrays | OddOccurrencesInArray | 100 | - | +| 7-Stacks and Queues | StoneWall | 85 | 정확도 100%, 성능 77% | From d1c9fff17bfe696a6c60785f8dce75ed811a20a3 Mon Sep 17 00:00:00 2001 From: grap3fruit Date: Tue, 5 Jan 2021 21:46:01 +0900 Subject: [PATCH 07/20] =?UTF-8?q?15=EC=A3=BC=EC=B0=A8=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/grap3fruit/js/goorm/12981.js | 34 ++++++++++++++++++++ src/grap3fruit/js/goorm/42578.js | 26 +++++++++++++++ src/grap3fruit/js/goorm/43162.js | 54 ++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 src/grap3fruit/js/goorm/12981.js create mode 100644 src/grap3fruit/js/goorm/42578.js create mode 100644 src/grap3fruit/js/goorm/43162.js diff --git a/src/grap3fruit/js/goorm/12981.js b/src/grap3fruit/js/goorm/12981.js new file mode 100644 index 0000000..3a35eb8 --- /dev/null +++ b/src/grap3fruit/js/goorm/12981.js @@ -0,0 +1,34 @@ +const check = (words, n, word, idx, visited) => { + if (visited.includes(word)) { + return [(idx % n) + 1, Math.ceil((idx + 1) / n)]; + } + if (idx > 0) { + if (words[idx - 1][words[idx - 1].length - 1] !== word[0]) { + return [(idx % n) + 1, Math.ceil((idx + 1) / n)]; + } + } + return null; +}; + +function solution(n, words) { + let answer = null; + const visited = []; + + words.forEach((word, idx) => { + if (!answer) { + answer = check(words, n, word, idx, visited); + visited.push(word); + } + }); + 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']; + +solution(n, words); diff --git a/src/grap3fruit/js/goorm/42578.js b/src/grap3fruit/js/goorm/42578.js new file mode 100644 index 0000000..3ec1378 --- /dev/null +++ b/src/grap3fruit/js/goorm/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/goorm/43162.js b/src/grap3fruit/js/goorm/43162.js new file mode 100644 index 0000000..bcd9b98 --- /dev/null +++ b/src/grap3fruit/js/goorm/43162.js @@ -0,0 +1,54 @@ +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) => { + let visitFlag = false; + visited.forEach((el) => { + if (el.includes(idx)) { + visitFlag = true; + } + }); + + 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); From b83a89af6b2f0a44e1000b0aeb4851c592ac0a12 Mon Sep 17 00:00:00 2001 From: grap3fruit Date: Tue, 5 Jan 2021 21:49:28 +0900 Subject: [PATCH 08/20] =?UTF-8?q?=EC=9C=84=EC=9E=A5=EB=AC=B8=EC=A0=9C=20ma?= =?UTF-8?q?p=EC=9C=BC=EB=A1=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/grap3fruit/js/goorm/42578_2.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/grap3fruit/js/goorm/42578_2.js diff --git a/src/grap3fruit/js/goorm/42578_2.js b/src/grap3fruit/js/goorm/42578_2.js new file mode 100644 index 0000000..f7b369c --- /dev/null +++ b/src/grap3fruit/js/goorm/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)); From 90776e17d789982eb0e58646e07c0f3533c7cf33 Mon Sep 17 00:00:00 2001 From: do02reen24 Date: Wed, 6 Jan 2021 00:36:01 +0900 Subject: [PATCH 09/20] solve : codility lesson3 problems --- .../codility/3_FrogJmp.js" | 3 +++ .../codility/3_PermMissingElem.js" | 12 ++++++++++ .../codility/3_TapeEquilibrium.js" | 13 +++++++++++ .../codility/score.md" | 23 ++++++++++++++----- 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 "src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_FrogJmp.js" create mode 100644 "src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_PermMissingElem.js" create mode 100644 "src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/3_TapeEquilibrium.js" 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..8d5da62 --- /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; 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/score.md" "b/src/do02reen24/\354\213\244\354\240\204\354\227\260\354\212\265/codility/score.md" index 4716041..59475bf 100644 --- "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" @@ -1,8 +1,19 @@ # 📋 Codility 점수 기록 -| Lesson | 문제 | 점수 | 비고 | -| :-----------------: | :-------------------: | :--: | :-------------------: | -| 1-Iterations | BinaryGap | 100 | - | -| 2-Arrays | CyclicRotation | 100 | - | -| 2-Arrays | OddOccurrencesInArray | 100 | - | -| 7-Stacks and Queues | StoneWall | 85 | 정확도 100%, 성능 77% | +| 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 | 84 | 정확도 71%, 성능 100% | 2021-01-06 | +| 7-Stacks and Queues | StoneWall | 85 | 정확도 100%, 성능 77% | 2021-01-05 | + +# 📚 Perfect Lesson + +> 모든 문제를 100%로 끝낸 Lesson + +* Lesson1 - Iterations + +* Lesson2 - Arrays \ No newline at end of file From 0dc189b003d55d98df6ae7d301f3b569ae551bcc Mon Sep 17 00:00:00 2001 From: do02reen24 Date: Wed, 6 Jan 2021 00:42:35 +0900 Subject: [PATCH 10/20] =?UTF-8?q?solve=20:=20tape=20equilibrium=20-=20?= =?UTF-8?q?=EA=B8=B0=EC=A1=B4=20=EB=A1=9C=EC=A7=81=EC=97=90=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=EA=B0=80=20=EC=9E=88=EC=9D=8C=EC=9D=84=20=EB=B0=9C?= =?UTF-8?q?=EA=B2=AC=ED=95=98=EA=B3=A0=20=EC=9D=B4=EB=A5=BC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=ED=95=B4=EC=A3=BC=EC=97=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codility/3_TapeEquilibrium.js" | 2 +- .../codility/score.md" | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) 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" index 8d5da62..3fc11e9 100644 --- "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" @@ -2,7 +2,7 @@ const solution = (A) => { let answer; let frontSum = 0; let backSum = A.reduce((a, b) => a + b, 0); - for (let i = 0; i < A.length; i += 1) { + for (let i = 0; i < A.length - 1; i += 1) { frontSum += A[i]; backSum -= A[i]; const absSub = Math.abs(frontSum - backSum); 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" index 59475bf..2debed9 100644 --- "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" @@ -7,7 +7,7 @@ | 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 | 84 | 정확도 71%, 성능 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 @@ -16,4 +16,6 @@ * Lesson1 - Iterations -* Lesson2 - Arrays \ No newline at end of file +* Lesson2 - Arrays +* Lesson3 - Time Complexity + From 649fefb376b2615d7941b1a3001fa810c7626943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=AF=E1=86=AB=E1=84=8B=E1=85=A7=E1=86=BC?= =?UTF-8?q?=E1=84=8B=E1=85=A5=E1=86=AB?= Date: Wed, 6 Jan 2021 01:19:59 +0900 Subject: [PATCH 11/20] =?UTF-8?q?solve:=20programmers=20=EB=84=A4=ED=8A=B8?= =?UTF-8?q?=EC=9B=8C=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../index.js" | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 "src/kyu9341/programmers_\353\204\244\355\212\270\354\233\214\355\201\254/index.js" 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))); +})(); From 473584890b0524fa40bc3cd84ceb4aba0e7da526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=AF=E1=86=AB=E1=84=8B=E1=85=A7=E1=86=BC?= =?UTF-8?q?=E1=84=8B=E1=85=A5=E1=86=AB?= Date: Wed, 6 Jan 2021 01:20:34 +0900 Subject: [PATCH 12/20] =?UTF-8?q?solve:=20programmers=20=EC=9C=84=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../index.js" | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 "src/kyu9341/programmers_\354\234\204\354\236\245/index.js" 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))); +})(); From 31950c5a8213a1bf602e28181a6ee01fca5ee211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=AF=E1=86=AB=E1=84=8B=E1=85=A7=E1=86=BC?= =?UTF-8?q?=E1=84=8B=E1=85=A5=E1=86=AB?= Date: Wed, 6 Jan 2021 01:21:23 +0900 Subject: [PATCH 13/20] =?UTF-8?q?solve:=20programmers=20=EC=98=81=EC=96=B4?= =?UTF-8?q?=EB=81=9D=EB=A7=90=EC=9E=87=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../index.js" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "src/kyu9341/programmers_\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260/index.js" 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))); +})(); From dc58fe572e5083e0e7e70ebc895832683953df33 Mon Sep 17 00:00:00 2001 From: Do-ho Date: Wed, 6 Jan 2021 18:52:23 +0900 Subject: [PATCH 14/20] =?UTF-8?q?solve:=20programmers=20=EC=9C=84=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 조합을 통한 풀이 --- .../pgs_\354\234\204\354\236\245.js" | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 "src/Do-ho/dataStructor/pgs_\354\234\204\354\236\245/pgs_\354\234\204\354\236\245.js" 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 From 4dda60932d233a1f84571b9dfda2670b155f1566 Mon Sep 17 00:00:00 2001 From: Do-ho Date: Wed, 6 Jan 2021 18:52:37 +0900 Subject: [PATCH 15/20] =?UTF-8?q?solve:=20programmers=20=EB=84=A4=ED=8A=B8?= =?UTF-8?q?=EC=9B=8C=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - dfs를 통한 풀이 --- ...44\355\212\270\354\233\214\355\201\254.js" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "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" 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 From 448f895d96b9d3af41583d15d0a5ccc3c6ee048d Mon Sep 17 00:00:00 2001 From: Do-ho Date: Wed, 6 Jan 2021 18:52:48 +0900 Subject: [PATCH 16/20] =?UTF-8?q?solve:=20programmers=20=EC=98=81=EC=96=B4?= =?UTF-8?q?=EB=81=9D=EB=A7=90=EC=9E=87=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 단순 구현 문제 --- ...35\353\247\220\354\236\207\352\270\260.js" | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 "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" 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 Date: Wed, 6 Jan 2021 18:55:33 +0900 Subject: [PATCH 17/20] =?UTF-8?q?docs:=20PR=20Template=20=EC=97=85?= =?UTF-8?q?=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/PULL_REQUEST_TEMPLATE.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md 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 From 144e43fd5ed088df53e9f7d64dcfcbd9228b0961 Mon Sep 17 00:00:00 2001 From: grap3fruit Date: Wed, 6 Jan 2021 20:39:33 +0900 Subject: [PATCH 18/20] =?UTF-8?q?15=ED=9A=8C=EC=B0=A8=20readme=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/grap3fruit/js/programmers/12981.js | 34 +++++++++++++++ src/grap3fruit/js/programmers/12981.md | 11 +++++ src/grap3fruit/js/programmers/42578.js | 26 ++++++++++++ src/grap3fruit/js/programmers/42578.md | 9 ++++ src/grap3fruit/js/programmers/42578_2.js | 29 +++++++++++++ src/grap3fruit/js/programmers/43162.js | 54 ++++++++++++++++++++++++ src/grap3fruit/js/programmers/43162.md | 11 +++++ 7 files changed, 174 insertions(+) create mode 100644 src/grap3fruit/js/programmers/12981.js create mode 100644 src/grap3fruit/js/programmers/12981.md create mode 100644 src/grap3fruit/js/programmers/42578.js create mode 100644 src/grap3fruit/js/programmers/42578.md create mode 100644 src/grap3fruit/js/programmers/42578_2.js create mode 100644 src/grap3fruit/js/programmers/43162.js create mode 100644 src/grap3fruit/js/programmers/43162.md diff --git a/src/grap3fruit/js/programmers/12981.js b/src/grap3fruit/js/programmers/12981.js new file mode 100644 index 0000000..3a35eb8 --- /dev/null +++ b/src/grap3fruit/js/programmers/12981.js @@ -0,0 +1,34 @@ +const check = (words, n, word, idx, visited) => { + if (visited.includes(word)) { + return [(idx % n) + 1, Math.ceil((idx + 1) / n)]; + } + if (idx > 0) { + if (words[idx - 1][words[idx - 1].length - 1] !== word[0]) { + return [(idx % n) + 1, Math.ceil((idx + 1) / n)]; + } + } + return null; +}; + +function solution(n, words) { + let answer = null; + const visited = []; + + words.forEach((word, idx) => { + if (!answer) { + answer = check(words, n, word, idx, visited); + visited.push(word); + } + }); + 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']; + +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..bcd9b98 --- /dev/null +++ b/src/grap3fruit/js/programmers/43162.js @@ -0,0 +1,54 @@ +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) => { + let visitFlag = false; + visited.forEach((el) => { + if (el.includes(idx)) { + visitFlag = true; + } + }); + + 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를 구한 후 개수를 세면 끝. From e00c6912882488de8787aa3fbcf91295161a2fae Mon Sep 17 00:00:00 2001 From: grap3fruit Date: Wed, 6 Jan 2021 20:40:12 +0900 Subject: [PATCH 19/20] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=9E=98=EB=AA=BB?= =?UTF-8?q?=20=EB=84=A3=EC=9D=80=EA=B2=83=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/grap3fruit/js/goorm/12981.js | 34 ------------------- src/grap3fruit/js/goorm/42578.js | 26 -------------- src/grap3fruit/js/goorm/42578_2.js | 29 ---------------- src/grap3fruit/js/goorm/43162.js | 54 ------------------------------ 4 files changed, 143 deletions(-) delete mode 100644 src/grap3fruit/js/goorm/12981.js delete mode 100644 src/grap3fruit/js/goorm/42578.js delete mode 100644 src/grap3fruit/js/goorm/42578_2.js delete mode 100644 src/grap3fruit/js/goorm/43162.js diff --git a/src/grap3fruit/js/goorm/12981.js b/src/grap3fruit/js/goorm/12981.js deleted file mode 100644 index 3a35eb8..0000000 --- a/src/grap3fruit/js/goorm/12981.js +++ /dev/null @@ -1,34 +0,0 @@ -const check = (words, n, word, idx, visited) => { - if (visited.includes(word)) { - return [(idx % n) + 1, Math.ceil((idx + 1) / n)]; - } - if (idx > 0) { - if (words[idx - 1][words[idx - 1].length - 1] !== word[0]) { - return [(idx % n) + 1, Math.ceil((idx + 1) / n)]; - } - } - return null; -}; - -function solution(n, words) { - let answer = null; - const visited = []; - - words.forEach((word, idx) => { - if (!answer) { - answer = check(words, n, word, idx, visited); - visited.push(word); - } - }); - 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']; - -solution(n, words); diff --git a/src/grap3fruit/js/goorm/42578.js b/src/grap3fruit/js/goorm/42578.js deleted file mode 100644 index 3ec1378..0000000 --- a/src/grap3fruit/js/goorm/42578.js +++ /dev/null @@ -1,26 +0,0 @@ -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/goorm/42578_2.js b/src/grap3fruit/js/goorm/42578_2.js deleted file mode 100644 index f7b369c..0000000 --- a/src/grap3fruit/js/goorm/42578_2.js +++ /dev/null @@ -1,29 +0,0 @@ -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/goorm/43162.js b/src/grap3fruit/js/goorm/43162.js deleted file mode 100644 index bcd9b98..0000000 --- a/src/grap3fruit/js/goorm/43162.js +++ /dev/null @@ -1,54 +0,0 @@ -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) => { - let visitFlag = false; - visited.forEach((el) => { - if (el.includes(idx)) { - visitFlag = true; - } - }); - - 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); From f13aa2c33a839e5fd01bdcce1cf684e8e54dc0e0 Mon Sep 17 00:00:00 2001 From: grap3fruit Date: Wed, 6 Jan 2021 21:49:34 +0900 Subject: [PATCH 20/20] =?UTF-8?q?15=EC=A3=BC=EC=B0=A8=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/grap3fruit/js/programmers/12981.js | 22 +++++++++++++--------- src/grap3fruit/js/programmers/43162.js | 7 +------ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/grap3fruit/js/programmers/12981.js b/src/grap3fruit/js/programmers/12981.js index 3a35eb8..57214da 100644 --- a/src/grap3fruit/js/programmers/12981.js +++ b/src/grap3fruit/js/programmers/12981.js @@ -1,10 +1,14 @@ -const check = (words, n, word, idx, visited) => { - if (visited.includes(word)) { - return [(idx % n) + 1, Math.ceil((idx + 1) / n)]; +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 (words[idx - 1][words[idx - 1].length - 1] !== word[0]) { - return [(idx % n) + 1, Math.ceil((idx + 1) / n)]; + if (lastWord[lastWord.length - 1] !== word[0]) { + return getResult(idx, n); } } return null; @@ -12,12 +16,12 @@ const check = (words, n, word, idx, visited) => { function solution(n, words) { let answer = null; - const visited = []; + const visited = new Map(); words.forEach((word, idx) => { if (!answer) { - answer = check(words, n, word, idx, visited); - visited.push(word); + answer = check(words[idx - 1], n, word, idx, visited); + visited.set(word, true); } }); if (!answer) { @@ -31,4 +35,4 @@ const words = ['hello', 'two']; // const words = ['hello', 'one', 'even', 'never', 'now', 'world', 'draw']; // const words = ['tank', 'kick', 'know', 'wheel', 'land', 'dream', 'mother', 'robot', 'tank']; -solution(n, words); +console.log(solution(n, words)); diff --git a/src/grap3fruit/js/programmers/43162.js b/src/grap3fruit/js/programmers/43162.js index bcd9b98..0affc25 100644 --- a/src/grap3fruit/js/programmers/43162.js +++ b/src/grap3fruit/js/programmers/43162.js @@ -21,12 +21,7 @@ function solution(n, computers) { const visited = []; computers.forEach((_, idx) => { - let visitFlag = false; - visited.forEach((el) => { - if (el.includes(idx)) { - visitFlag = true; - } - }); + const visitFlag = visited.some((el) => el.includes(idx)); if (!visitFlag) { const newVisited = bfs(computers, idx);