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/README.md b/README.md index 9bf110f..ff99bcb 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ | 11์ฃผ์ฐจ | [์—ด์ฐจ ์‹œ๊ฐ„ํ‘œ](https://www.acmicpc.net/problem/12731) | [์ •์ˆ˜ ์‚ผ๊ฐํ˜•](https://www.acmicpc.net/problem/1932) | [ํŒฐ๋ฆฐ๋“œ๋กฌ ๋งŒ๋“ค๊ธฐ](https://www.acmicpc.net/problem/1254) | | | 12์ฃผ์ฐจ | [ATM](https://www.acmicpc.net/problem/11399) | [ํšŒ๋ฌธ](https://www.acmicpc.net/problem/17609) | | | | 13์ฃผ์ฐจ | [๋ฏธ๋กœํƒ์ƒ‰](https://www.acmicpc.net/problem/2178) | [์•”ํ˜ธ์ฝ”๋“œ](https://www.acmicpc.net/problem/2011) | [๋–ก๋จน๋Š”ํ˜ธ๋ž‘์ด](https://www.acmicpc.net/problem/2502) | [์ดŒ์ˆ˜๊ณ„์‚ฐ](https://www.acmicpc.net/problem/2644) | - +| 14์ฃผ์ฐจ | [ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ](https://programmers.co.kr/learn/courses/30/lessons/64061) | [๊ฐ€์žฅ ๊ธด ํŒฐ๋ฆฐ๋“œ๋กฌ](https://programmers.co.kr/learn/courses/30/lessons/12904) | [ํŠœํ”Œ](https://programmers.co.kr/learn/courses/30/lessons/64065) | | +| 15์ฃผ์ฐจ | [์œ„์žฅ](https://programmers.co.kr/learn/courses/30/lessons/42578) | [๋„คํŠธ์›Œํฌ](https://programmers.co.kr/learn/courses/30/lessons/43162) | [์˜์–ด ๋๋ง์ž‡๊ธฐ](https://programmers.co.kr/learn/courses/30/lessons/12981) | | ## :blue_book: Additional Study | ๋ถ„๋ฅ˜ | | | | diff --git a/contents/200905.md b/contents/200905.md deleted file mode 100644 index d29072b..0000000 --- a/contents/200905.md +++ /dev/null @@ -1,11 +0,0 @@ -# :fire: ๊ธˆ์ฃผ์˜ ๋ฌธ์ œ (200905) - -> ์ด๋ฒˆ ์ฃผ ์ฃผ์ œ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. - -https://www.acmicpc.net/problem/1158 - -https://www.acmicpc.net/problem/1406 - -https://www.acmicpc.net/problem/2346 - -https://www.acmicpc.net/problem/2957 \ No newline at end of file diff --git a/contents/README.md b/contents/README.md deleted file mode 100644 index 8d294c7..0000000 --- a/contents/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# :fire: ๊ธˆ์ฃผ์˜ ๋ฌธ์ œ (200912) - -> ์ด๋ฒˆ ์ฃผ ์ฃผ์ œ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ 2ํƒ„์ž…๋‹ˆ๋‹ค. - -https://www.acmicpc.net/problem/11286 - -https://programmers.co.kr/learn/courses/30/lessons/42578 - -https://www.acmicpc.net/problem/1991 - -https://www.acmicpc.net/problem/2957 diff --git a/mom/mom20200905.md b/mom/mom20200905.md deleted file mode 100644 index ca4e8ee..0000000 --- a/mom/mom20200905.md +++ /dev/null @@ -1,23 +0,0 @@ -# :family_man_man_girl_boy: ํšŒ์˜๋ก - -### :question: ๊ฐœ์ธ์˜ ์ƒํ™œ์— ์žˆ์–ด์„œ ๋ฏธ์…˜์— ํž˜๋“ฆ์ด ์žˆ์—ˆ๋‚˜. - -- ๋ฏธ์…˜์— ์—ด์ค‘ํ•˜๋А๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•  ์‹œ๊ฐ„์ด ์—†๊ธด ํ–ˆ์œผ๋‚˜ ์‰ฌ์šด 3 ๋ฌธ์ œ, ์–ด๋ ค์šด 1 ๋ฌธ์ œ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์€ ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค. - -- ํ–ฅํ›„ ๊ณ„์†์ ์ธ ์กฐ์œจ์˜ ์‹œ๊ฐ„์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค. - - - -### :hammer: ํ–ฅํ›„ ํ”ผ์–ด ์„ธ์…˜ ๊ณ„ํš - -- **10:00 ~ 10:15** ์Šคํ„ฐ๋””์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ - -- **10:15 ~ 11:50** ์ฝ”๋“œ ๋ถ„์„ ๋ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต์œ  - -- **11:50 ~ 12:00** ๋‹ค์Œ ์ฃผ์ œ ์„ ์ • ๋ฐ ๋ฌธ์ œ ๊ณต์œ  - - - -### :bell: ๊ธฐํƒ€ - -- 2957๋ฒˆ ๋ฌธ์ œ์™€ ๊ฐ™์€ ๊ฒฝ์šฐ ๋‹ค์Œ ์ฃผ๊นŒ์ง€ ๋‹ค์‹œ ํ’€์–ด์˜ค๊ธฐ \ 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_\352\260\200\354\236\245\352\270\264\355\214\260\353\246\260\353\223\234\353\241\254/pgs_palindrome.js" "b/src/Do-ho/implementation/pgs_\352\260\200\354\236\245\352\270\264\355\214\260\353\246\260\353\223\234\353\241\254/pgs_palindrome.js" new file mode 100644 index 0000000..ce7affc --- /dev/null +++ "b/src/Do-ho/implementation/pgs_\352\260\200\354\236\245\352\270\264\355\214\260\353\246\260\353\223\234\353\241\254/pgs_palindrome.js" @@ -0,0 +1,21 @@ +const getOddPalindromeLength = (s, idx) => { return loopPalindrome(s, idx-1, idx+1, 1); } +const getEvenPalindromeLength = (s, idx) => { + if(s[idx] != s[idx+1]) return 0; + return loopPalindrome(s, idx-1, idx+2, 2); +} + +const loopPalindrome = (s, left, right, palindromeCount) => { + while(left >= 0 && right < s.length) { + if(s[left--] !== s[right++]) break; + palindromeCount += 2 + } + return palindromeCount; +} + +const solution = s => { + let answer = 0; + for (let i=0; i { + 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 { + for(let i=0; i { + let answer = 0; + let basket = new CraneStack(); + + moves.forEach((item) => { + const doll = pickDoll(board, item-1); + if(doll!==0) answer += basket.pushAndGetResult(doll); + }) + + return answer; +} \ No newline at end of file diff --git "a/src/Do-ho/implementation/pgs_\355\212\234\355\224\214/pgs_tuple.js" "b/src/Do-ho/implementation/pgs_\355\212\234\355\224\214/pgs_tuple.js" new file mode 100644 index 0000000..b2f5b43 --- /dev/null +++ "b/src/Do-ho/implementation/pgs_\355\212\234\355\224\214/pgs_tuple.js" @@ -0,0 +1,25 @@ +const removeBracket = s => { + const re = /[0-9]+(,[0-9]+)*/g; + return s.match(re); +} + +const compareFunction = (prev, curr) => prev.length - curr.length; + +const parseStr = s => { + const strWithOutBracket = removeBracket(s); + const parsed = strWithOutBracket.reduce((acc, cur) => { + acc.push(cur.split(',')); + return acc; + }, []) + return parsed.sort(compareFunction); +} + +const solution = s => { + const parsedArr = parseStr(s); + const answer = parsedArr.reduce((acc, cur) => { + const filterItem = cur.filter((item) => !acc.includes(item)); + acc.push(filterItem[0]); + return acc; + }, []).map(item => parseInt(item)) + return answer; +} \ No newline at end of file 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 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/week13.md b/src/do02reen24/Review/week13.md index 8624359..d9f5559 100644 --- a/src/do02reen24/Review/week13.md +++ b/src/do02reen24/Review/week13.md @@ -1,4 +1,4 @@ -# :fire: week12 +# :fire: week13 ## :ballot_box_with_check: ๋ฐฑ์ค€ 2011 diff --git a/src/do02reen24/Review/week14.md b/src/do02reen24/Review/week14.md new file mode 100644 index 0000000..2c7d078 --- /dev/null +++ b/src/do02reen24/Review/week14.md @@ -0,0 +1,119 @@ +# :fire: week14 + +- ์ด๋ฒˆ์ฃผ๋Š” ๋งˆ์ด๋ฆฌ์–ผํŠธ๋ฆฝ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ค€๋น„๋ฅผ ์œ„ํ•ด ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ `javascript`๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ๋กœ ํ•˜์˜€๋‹ค. + +## :ballot_box_with_check: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํŠœํ”Œ(64065) + +- python์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€๋‹ค๊ฐ€ javascript๋กœ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋‹ˆ ํ—ท๊ฐˆ๋ ธ๋‹ค. +- ๋ณ€์ˆ˜๋ช…๊ณผ ํ•จ์ˆ˜ ๋ถ„๋ฆฌ๋ฅผ ์‹ ๊ฒฝ์จ์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค. + +## :ballot_box_with_check: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ฒŒ์ž„(64061) + +#### ์ •ํ™•์„ฑ 81.8 + +- ํ…Œ์ŠคํŠธ 1๋ฒˆ, 2๋ฒˆ ์‹คํŒจ + +```js +const getTopDollIndex = (board, index) => { + let topIndex = -1; + for (const line of board) { + topIndex = topIndex + 1; + if (line[index] !== 0) break; + } + return topIndex; +}; + +const solution = (board, moves) => { + let answer = 0; + const basket = []; + for (const move of moves) { + const dollX = move - 1; + const dollY = getTopDollIndex(board, dollX); + const doll = board[dollY][dollX]; + board[dollY][dollX] = 0; + + const top = basket.pop(); + if (top === doll) { + answer = answer + 2; + } else { + basket.push(top); + basket.push(doll); + } + } + return answer; +}; +``` + +- ํ•ด๋‹น ์—ด์— ๋‚จ์€ ์ธํ˜•์ด ์—†์„ ๊ฒฝ์šฐ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ์–ด์•ผ ํ–ˆ๋Š”๋ฐ ๊ธฐ์กด์˜ ์ฝ”๋“œ๋Š” 0์„ ๋„ฃ์–ด์ฃผ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Œ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. + +## :ballot_box_with_check: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ฐ€์žฅ ๊ธด ํŒฐ๋ฆฐ๋“œ๋กฌ(12904) + +#### ์ •ํ™•์„ฑ: 62.7, ํšจ์œจ์„ฑ: 30.7 + +- ํ…Œ์ŠคํŠธ 6๋ฒˆ 12๋ฒˆ ์‹คํŒจ + +```js +const isSameWord = (word, pre, next) => { + if (pre < 0 || next >= word.length) { + return false; + } + if (word[pre] !== word[next]) { + return false; + } + return true; +}; + +const oddPalindrome = (word, start) => { + let index = 1; + while (true) { + const pre = start - index; + const next = start + index; + if (isSameWord(word, pre, next)) { + index = index + 1; + continue; + } + index = index - 1; + break; + } + return index * 2 + 1; +}; + +const evenPalindrome = (word, start) => { + let index = 0; + while (true) { + const pre = start - index; + const next = start + 1 + index; + if (isSameWord(word, pre, next)) { + index = index + 1; + continue; + } + break; + } + return index * 2; +}; + +const isPalindrome = (word, start) => { + let length = oddPalindrome(word, start); + if (length === 1) { + const result = evenPalindrome(word, start); + if (result > length) { + length = result; + } + } + return length; +}; + +const solution = (s) => { + let maxLength = 0; + for (let index = 0; index < s.length; index++) { + const result = isPalindrome(s, index); + if (maxLength < result) { + maxLength = result; + } + } + + return maxLength; +}; +``` + +- ์ฒ˜์Œ ์ฝ”๋“œ์—์„œ๋Š” ํ™€์ˆ˜ ๊ฒ€์‚ฌ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ง์ˆ˜ ๊ฒ€์‚ฌ๋ฅผ ์ง„ํ–‰ํ•˜์ง€ ์•Š์•˜์—ˆ๋Š”๋ฐ, ์ง์ˆ˜ ๊ฒ€์‚ฌ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋” ํฐ ๊ฒฝ์šฐ๋„ ์กด์žฌํ•จ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‘ ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ๊ณ„์‚ฐํ•ด์ฃผ๊ณ  ๋” ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋„๋ก ๋ณ€๊ฒฝํ•˜์˜€๋‹ค. 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/simulation/programmers_64061.js b/src/do02reen24/simulation/programmers_64061.js new file mode 100644 index 0000000..cc390b6 --- /dev/null +++ b/src/do02reen24/simulation/programmers_64061.js @@ -0,0 +1,35 @@ +const getTopDollIndex = (board, index) => { + let topIndex = -1; + for (const line of board) { + topIndex = topIndex + 1; + if (line[index] !== 0) { + return topIndex; + } + } + return -1; +}; + +const solution = (board, moves) => { + let answer = 0; + const basket = []; + + for (const move of moves) { + const dollX = move - 1; + const dollY = getTopDollIndex(board, dollX); + if (dollY === -1) continue; + + const doll = board[dollY][dollX]; + board[dollY][dollX] = 0; + + const top = basket.pop(); + if (top === doll) { + answer = answer + 2; + continue; + } else if (top) { + basket.push(top); + } + basket.push(doll); + } + + return answer; +}; diff --git a/src/do02reen24/string/programmers_12904.js b/src/do02reen24/string/programmers_12904.js new file mode 100644 index 0000000..7547c24 --- /dev/null +++ b/src/do02reen24/string/programmers_12904.js @@ -0,0 +1,56 @@ +const isSameWord = (word, pre, next) => { + if (pre < 0 || next >= word.length) { + return false; + } + if (word[pre] !== word[next]) { + return false; + } + return true; +}; + +const oddPalindrome = (word, start) => { + let index = 1; + while (true) { + const pre = start - index; + const next = start + index; + if (isSameWord(word, pre, next)) { + index = index + 1; + continue; + } + index = index - 1; + break; + } + return index * 2 + 1; +}; + +const evenPalindrome = (word, start) => { + let index = 0; + while (true) { + const pre = start - index; + const next = start + 1 + index; + if (isSameWord(word, pre, next)) { + index = index + 1; + continue; + } + break; + } + return index * 2; +}; + +const isPalindrome = (word, start) => { + const oddResult = oddPalindrome(word, start); + const evenResult = evenPalindrome(word, start); + return Math.max(oddResult, evenResult); +}; + +const solution = (s) => { + let maxLength = 0; + for (let index = 0; index < s.length; index++) { + const result = isPalindrome(s, index); + if (maxLength < result) { + maxLength = result; + } + } + + return maxLength; +}; diff --git a/src/do02reen24/string/programmers_64065.js b/src/do02reen24/string/programmers_64065.js new file mode 100644 index 0000000..df4a831 --- /dev/null +++ b/src/do02reen24/string/programmers_64065.js @@ -0,0 +1,32 @@ +const countingNumber = (arr) => { + const counts = {}; + arr.forEach((n) => (counts[n] = (counts[n] || 0) + 1)); + return counts; +}; + +const sortByValue = (dict) => { + const arr = []; + for (const key in dict) { + const value = dict[key]; + arr.push([Number(key), value]); + } + return arr.sort((pre, next) => next[1] - pre[1]); +}; + +const getKeyList = (arr) => { + const keys = []; + for (const pair of arr) { + const key = pair[0]; + keys.push(key); + } + return keys; +}; + +const solution = (s) => { + const deleteBracket = s.replace(/[{}]/gi, ''); + const numberList = deleteBracket.split(','); + const counts = countingNumber(numberList); + const valueList = sortByValue(counts); + const answer = getKeyList(valueList); + return answer; +}; 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/boj/input1_1.js b/src/grap3fruit/js/boj/input1_1.js new file mode 100644 index 0000000..bdf20f4 --- /dev/null +++ b/src/grap3fruit/js/boj/input1_1.js @@ -0,0 +1,16 @@ +const solution = (N, data) => { + console.log(N); + console.log(data); +}; + +let fs = require('fs'); +let input = fs.readFileSync('test').toString().split('\n'); + +const N = +input[0]; +const data = []; +for (let i = 1; i < N + 1; i++) { + // ์œ„์—์„œ N์„ ๋ฐ›์„๋–„ input[0]์ด ๋น ์ ธ๋‚˜๊ฐ”๊ธฐ ๋•Œ๋ฌธ์— 1~N์„ ๋ฐ›์•„์•ผํ•œ๋‹ค. + data.push(input[i].split(' ').map((el) => +el)); +} + +solution(N, data); diff --git a/src/grap3fruit/js/boj/input1_2.js b/src/grap3fruit/js/boj/input1_2.js new file mode 100644 index 0000000..cf99e7a --- /dev/null +++ b/src/grap3fruit/js/boj/input1_2.js @@ -0,0 +1,19 @@ +const solution = (N, info, data) => { + console.log(N); + const [X, Y] = info; + console.log(X, Y); + console.log(data); +}; + +let fs = require('fs'); +let input = fs.readFileSync('test2').toString().split('\n'); + +const N = +input[0]; +const info = input[1].split(' ').map((el) => +el); +const data = []; +for (let i = 2; i < N + 2; i++) { + // ์œ„์—์„œ N์„ ๋ฐ›์„๋–„ input[0]์ด ๋น ์ ธ๋‚˜๊ฐ”๊ธฐ ๋•Œ๋ฌธ์— 1~N์„ ๋ฐ›์•„์•ผํ•œ๋‹ค. + data.push(input[i].split(' ').map((el) => +el)); +} + +solution(N, info, data); diff --git a/src/grap3fruit/js/boj/test b/src/grap3fruit/js/boj/test new file mode 100644 index 0000000..80de847 --- /dev/null +++ b/src/grap3fruit/js/boj/test @@ -0,0 +1,4 @@ +3 +1 2 3 +2 3 4 +3 4 5 \ No newline at end of file diff --git a/src/grap3fruit/js/boj/test2 b/src/grap3fruit/js/boj/test2 new file mode 100644 index 0000000..67313fa --- /dev/null +++ b/src/grap3fruit/js/boj/test2 @@ -0,0 +1,5 @@ +3 +2 3 +1 2 3 +2 3 4 +3 4 5 \ No newline at end of file diff --git a/src/grap3fruit/js/doublyLinkedList.js b/src/grap3fruit/js/doublyLinkedList.js new file mode 100644 index 0000000..ead5907 --- /dev/null +++ b/src/grap3fruit/js/doublyLinkedList.js @@ -0,0 +1,108 @@ +class Node { + constructor(item) { + this.prev = null; + this.next = null; + this.item = item; + } +} + +class DoublyLinkedList { + constructor() { + this.head = new Node(null); + this.tail = new Node(null); + this.head.next = this.tail; + this.tail.prev = this.head; + this.length = 0; + } + + getLength() { + return this.length; + } + + push(item) { + const node = new Node(item); + this.length += 1; + + if (this.head.next === this.tail) { + node.prev = this.head; + node.next = this.tail; + this.head.next = node; + this.tail.prev = node; + return; + } + + let lastNode = this.tail.prev; + node.next = this.tail; + node.prev = lastNode; + lastNode.next = node; + this.tail.prev = node; + return; + } + + print() { + let current = this.head.next; + while (true) { + if (current === this.tail) { + return; + } + console.log(current.item); + current = current.next; + } + } + + pop() { + if (this.length === 0) { + return undefined; + } + this.length -= 1; + const popedNode = this.tail.prev; + popedNode.prev.next = this.tail; + this.tail.prev = popedNode.prev; + return popedNode.item; + } + + popLeft() { + if (this.length === 0) { + return undefined; + } + this.length -= 1; + const popedNode = this.head.next; + popedNode.next.prev = this.head; + this.head.next = popedNode.next; + return popedNode.item; + } + + filter(callback) { + let current = this.head.next; + const result = []; + while (current !== this.tail) { + console.log(current.item); + if (callback(current.item)) { + result.push(current.item); + } + current = current.next; + } + return result; + } +} + +const dl = new DoublyLinkedList(); + +dl.push([1, 0]); +dl.push([2, 1]); +dl.push([3, 1]); +dl.push([4, 2]); +dl.push([5, 2]); +console.log(dl.filter((el) => el[0] === 1 && el[1] === 0)); +console.log('---'); +console.log(dl.getLength()); +console.log('---'); +console.log(dl.popLeft()); +console.log('---'); +dl.print(); +console.log('---'); +console.log(dl.pop()); +console.log('---'); +console.log(dl.getLength()); +console.log('---'); +dl.print(); diff --git a/src/grap3fruit/js/goorm/input1.js b/src/grap3fruit/js/goorm/input1.js new file mode 100644 index 0000000..2acbab0 --- /dev/null +++ b/src/grap3fruit/js/goorm/input1.js @@ -0,0 +1,33 @@ +const solution = (N, data) => { + console.log(N); + console.log(data); +}; + +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +let N = null; +let count = 0; +const data = []; + +rl.on('line', function (line) { + console.log(line); + if (!N) { + N = +line; + } else { + // data.push(line.split(' ').map((el) => +el)); + // data.push(line.split('').map((el) => +el)); + // data.push(line.split('').map((el) => el)); + data.push(line); + } + count += 1; + if (count === N) { + rl.close(); + } +}).on('close', function () { + solution(N, data); + process.exit(); +}); diff --git a/src/grap3fruit/js/goorm/input1_1.js b/src/grap3fruit/js/goorm/input1_1.js new file mode 100644 index 0000000..de07578 --- /dev/null +++ b/src/grap3fruit/js/goorm/input1_1.js @@ -0,0 +1,23 @@ +const solution = (data) => { + console.log(data); +}; + +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +let data = []; + +rl.on('line', function (line) { + data.push(line); + // data = line.split('').map((el) => el); + // data = line.split(' ').map((el) => el); + // data = line.split('').map((el) => +el); + + rl.close(); +}).on('close', function () { + solution(data); + process.exit(); +}); diff --git a/src/grap3fruit/js/goorm/input1_2.js b/src/grap3fruit/js/goorm/input1_2.js new file mode 100644 index 0000000..ba0ba7e --- /dev/null +++ b/src/grap3fruit/js/goorm/input1_2.js @@ -0,0 +1,42 @@ +const solution = (N, info, data) => { + console.log(N); + const [X, Y] = info; + console.log(X, Y); + console.log(data); +}; + +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +let N = null; +let info = null; +let count = 0; +const data = []; + +rl.on('line', function (line) { + if (!N) { + // N์ด null์ด๋ฉด + N = +line; + } else if (!info) { + // N์ด null์ด ์•„๋‹Œ๋ฐ, info๊ฐ€ null์ด๋ฉด + info = line.split(' ').map((el) => +el); + } else { + // N๊ณผ info๊ฐ€ null์ด ์•„๋‹ˆ๋ฉด + data.push(line); + // data.push(line.split('').map((el) => +el)); + // data.push(line.split('').map((el) => el)); + // data.push(line.split(' ').map((el) => +el)); + count += 1; // data๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด count๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๊ณ  + } + if (count === N) { + // count๊ฐ€ ์ž…๋ ฅ๋ฐ›์•„์•ผํ•˜๋Š” N์ผ๋•Œ rl.close()๋ฅผ ํ˜ธ์ถœํ•ด์ค€๋‹ค. + rl.close(); + } +}).on('close', function () { + // rl.close()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ด ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ๋“ค์–ด์˜ค๊ณ  + solution(N, info, data); // solution์„ ์‹คํ–‰ ํ•œ ํ›„ + process.exit(); // ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค. +}); diff --git a/src/grap3fruit/js/goorm/input1_3.js b/src/grap3fruit/js/goorm/input1_3.js new file mode 100644 index 0000000..9784cef --- /dev/null +++ b/src/grap3fruit/js/goorm/input1_3.js @@ -0,0 +1,47 @@ +const solution = (N, data) => { + console.log(N); + console.log(data); +}; + +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +let T = null; +let N = null; +let info = null; +let countN = 0; +let countT = 0; +let data = []; + +rl.on('line', function (line) { + if (!T) { + T = +line; + } else if (!N) { + N = +line; + } else { + data.push(line); + // data.push(line.split('').map((el) => +el)); + // data.push(line.split('').map((el) => el)); + // data.push(line.split(' ').map((el) => +el)); + countN += 1; // data๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด countN์„ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๊ณ  + } + if (countN === N) { + // N๋งŒํผ data๋ฅผ ์ž˜ ์ž…๋ ฅ ๋ฐ›์•˜์œผ๋ฉด + solution(N, data); // solution์„ ํ˜ธ์ถœํ•˜๊ณ  + N = null; // T, countT๋ฅผ ์ œ์™ธํ•œ ๊ฐ’๋“ค์„ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค. + info = null; + countN = 0; + data = []; + + countT += 1; // ๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ํ•˜๋‚˜๋ฅผ ํ†ต๊ณผํ–ˆ์œผ๋‹ˆ countT๋ฅผ 1 ์˜ฌ๋ ค์ค€๋‹ค. + } + if (countT === T) { + // ์ž…๋ ฅ๋ฐ›์€ T ๋งŒํผ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜๊ฒŒ๋˜๋ฉด + rl.close(); // rl.close()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  + } +}).on('close', function () { + process.exit(); // ์ข…๋ฃŒํ•œ๋‹ค. +}); diff --git a/src/grap3fruit/js/goorm/input2.js b/src/grap3fruit/js/goorm/input2.js new file mode 100644 index 0000000..e52121d --- /dev/null +++ b/src/grap3fruit/js/goorm/input2.js @@ -0,0 +1,31 @@ +const solution = (N, data) => { + console.log(N); + console.log(data); +}; + +const readline = require('readline'); + +(async () => { + let rl = readline.createInterface({ input: process.stdin }); + let N = null; + let count = 0; + const data = []; + + for await (const line of rl) { + if (!N) { + N = +line; + } else { + data.push(line); // 1 2 3 4 5 -> ['1 2 3 4 5'] + // data.push(line.split(' ').map((el) => +el)); // 1 2 3 4 5 -> [1,2,3,4,5] + // data.push(line.split('').map((el) => el)); // 12345 -> ['1','2','3','4','5'] + // data.push(line.split('').map((el) => +el)); // 12345 -> [1,2,3,4,5] + count += 1; + } + if (N === count) { + rl.close(); + } + } + + solution(N, data); + process.exit(); +})(); diff --git a/src/grap3fruit/js/input.md b/src/grap3fruit/js/input.md new file mode 100644 index 0000000..e9036a9 --- /dev/null +++ b/src/grap3fruit/js/input.md @@ -0,0 +1 @@ +[๊ตฌ๋ฆ„(goorm), ๋ฐฑ์ค€(BOJ) ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ JavaScript๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ• ์ •๋ฆฌ](https://velog.io/@grap3fruit/๊ตฌ๋ฆ„goorm-์ฝ”ํ…Œ-javascript-๋กœ-์ž…๋ ฅ๊ฐ’-๋ฐ›๋Š”-๋ฐฉ๋ฒ•) ๋ธ”๋กœ๊ทธ ๊ธ€ ์ž‘์„ฑ diff --git a/src/grap3fruit/js/programmers/12904.js b/src/grap3fruit/js/programmers/12904.js new file mode 100644 index 0000000..ef69dc0 --- /dev/null +++ b/src/grap3fruit/js/programmers/12904.js @@ -0,0 +1,37 @@ +const getMaxOddPalindrome = (word, idx) => { + let max = 0; + for (let i = 0; i < word.length / 2; i++) { + if (word[idx - i] !== word[idx + i]) { + break; + } + max = i * 2 + 1; + } + return max; +}; + +const getMaxEvenPalindrome = (word, idx) => { + let max = 0; + if (idx < word.length - 1 && word[idx] === word[idx + 1]) { + for (let i = 0; i < word.length / 2; i++) { + if (word[idx - i] !== word[idx + i + 1]) { + break; + } + max = i * 2 + 2; + } + } + return max; +}; + +const solution = (s) => { + let sArr = s.split(''); + let answer = 0; + + for (let i = 0; i < sArr.length; i++) { + answer = Math.max(answer, getMaxOddPalindrome(sArr, i), getMaxEvenPalindrome(sArr, i)); + } + + return answer; +}; + +// solution('ccabcbaa'); +console.log(solution('baaba')); diff --git a/src/grap3fruit/js/programmers/12904.md b/src/grap3fruit/js/programmers/12904.md new file mode 100644 index 0000000..3638877 --- /dev/null +++ b/src/grap3fruit/js/programmers/12904.md @@ -0,0 +1,72 @@ +# 12904 ๊ฐ€์žฅ ๊ธด ํŒฐ๋ฆฐ๋“œ๋กฌ - ์„ฑ๊ณต + +## ์•„์ด๋””์–ด ๊ตฌํ˜„ + +์ฒซ๋ฒˆ์งธ ํ’€์ด + +``` +const checkPD = (word) => { + const wordLength = word.length; + + for (let i = 0; i < wordLength / 2; i++) { + if (word[i] !== word[wordLength - i - 1]) { + return false; + } + } + return true; +}; + +const solution = (s) => { + sArr = s.split(''); + answer = 0; + + sArr.reduce((accumulator, currentValue, currentIndex, array) => { + console.log(accumulator); + console.log(accumulator.join('')); + if (accumulator.length > 0 && checkPD(accumulator.join(''))) { + console.log(`${currentIndex}์—์„œ ํŽ ๋ฆฐ๋“œ๋กฌ์ž…๋‹ˆ๋‹ค`); + answer = currentIndex; + } + + return [...accumulator, currentValue]; + }, []); + console.log(sArr); + + if (checkPD(sArr.join(''))) { + // console.log(`${sArr.length}์—์„œ ํŽ ๋ฆฐ๋“œ๋กฌ์ž…๋‹ˆ๋‹ค`); + answer = sArr.length; + } + + console.log(answer); + return answer; +}; + +solution('abcabcdcbae'); +``` + +reduce์จ์„œ ๋ฉ‹์ง€๊ฒŒ ํ’€์–ด๋ณด๋ ค๊ณ  ํ–ˆ์ง€๋งŒ, + +ํŒฐ๋ฆฐ๋“œ๋กฌ์ด ์ค‘๊ฐ„์— ์˜ค๋Š” ๊ฒฝ์šฐ ๋ชป์ฐพ๋Š”๋‹ค. ํ•ญ์ƒ ์•ž์—์„œ ๋ถ™์—ฌ๋‚˜๊ฐ€๊ธฐ ๋•Œ๋ฌธ ใ…  + +ํ…Œ์ผ€ `abcabcdcbae` ์—ฌ๊ธฐ์— ๊ฑธ๋ฆผ +`adcbbbbca` ์ด๊ฒƒ๋„ ์•ˆ๋ ๋“ฏ. + +--- + +ํ•œ๊ฐœ์งœ๋ฆฌ์ผ๋•Œ ์ซ™ ์ฐพ๊ณ , + +๋‘๊ฐœ์งœ๋ฆฌ์ผ๋•Œ ์ซ™ ์ฐพ๊ณ  + +ํ•ด์•ผ๊ฒ ๋Š”๊ฑธ. + +๊ทผ๋ฐ ์ด๋Ÿฌ๋ฉด ํšจ์œจ์„ฑ ํ†ต๊ณผ ์•ˆ๋ ๊ฑฐ๊ฐ™์€๋ฐ. + +์œ„์— ๋А๋‚Œ์œผ๋กœ ํ•˜๋ ค๋ฉด, ํฐ๊ฑฐ๋ถ€ํ„ฐ ์ฐพ์ž. ์ž‘์€๊ฑด ๋ฌด์กฐ๊ฑด ํฐ๊ฑฐ๊นŒ์ง€ ๊ฐ€์•ผํ•˜์ง€๋งŒ ํฐ๊ฑฐ๋Š” ๋˜๋ฉด ๋ฐ”๋กœ ๋๋‚ด๋ฉด ๋œ๋‹ค. + +--- + +์œ„์—๊บผ๋Š” ์•„๋‹Œ๊ฒƒ ๊ฐ™์•„ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋– ์˜ค๋ฆ„. + +ํ•˜๋‚˜์”ฉ ๊ฐ€๋ฉด์„œ ๋„“ํ˜€๋ณด๊ธฐ. ์ตœ๋Œ€ํ•œ ๋„“ํžŒ max ๋ฆฌํ„ด. << ์ด ๋ฐฉ๋ฒ•. + +์•„์ด๋””์–ด ์‹ธ์›€์ด์—ˆ๋‹ค. ๋ชป๋– ์˜ฌ๋ ธ์œผ๋ฉด ์œผ์œผ.. 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/grap3fruit/js/programmers/64061.js b/src/grap3fruit/js/programmers/64061.js new file mode 100644 index 0000000..246ddd8 --- /dev/null +++ b/src/grap3fruit/js/programmers/64061.js @@ -0,0 +1,44 @@ +const board = [ + [0, 0, 0, 0, 0], + [0, 0, 1, 0, 3], + [0, 2, 5, 0, 1], + [4, 2, 4, 4, 2], + [3, 5, 1, 3, 1], +]; +const moves = [1, 5, 3, 5, 1, 2, 1, 4]; + +function solution(board, moves) { + const result = []; + let answer = 0; + + moves.reduce((acc, move) => { + for (let i = 0; i < board.length; i++) { + if (board[i][move - 1] === 0) { + continue; + } + + if (acc.length > 0) { + const prev = acc.pop(); + + if (prev === board[i][move - 1]) { + board[i][move - 1] = 0; + answer += 2; + return acc; + } + + acc.push(prev); + } + + acc.push(board[i][move - 1]); + board[i][move - 1] = 0; + return acc; + } + return acc; + }, []); + + console.log(board); + console.log(result); + return answer; +} + +console.log(solution(board, moves)); diff --git a/src/grap3fruit/js/programmers/64061.md b/src/grap3fruit/js/programmers/64061.md new file mode 100644 index 0000000..9229de2 --- /dev/null +++ b/src/grap3fruit/js/programmers/64061.md @@ -0,0 +1,25 @@ +# 64061 ํฌ๋ ˆ์ธ ์ธํ˜•๋ฝ‘๊ธฐ ๊ฒŒ์ž„ - ์„ฑ๊ณต + +## ๊ตฌํ˜„ + +moves๋Š” board์˜ x์ถ•์— ์ ‘๊ทผํ•˜๋Š”๋ฐ, + +์ด๊ฑธ ์ด์ œ result stack์— ๋„ฃ๊ณ , + +stack์— ๋„ฃ์„๋•Œ pop์„ ํ•œ๋ฒˆ ํ•ด์ค˜์„œ + +pop๋œ ์š”์†Œ๊ฐ€ ํ˜„์žฌ ๋„ฃ๋Š”๊ฒƒ๊ณผ ๋™์ผํ•˜๋ฉด pass ํ•˜๊ณ  answer์— +2 ํ•ด์คŒ. + +๋™์ผ ํ•˜์ง€ ์•Š์œผ๋ฉด pop ํ–ˆ๋˜๊ฑฐ ๋‹ค์‹œ ๋„ฃ๊ณ , ํ˜„์žฌ ์š”์†Œ ๋„ฃ์–ด์คŒ. + +--- + +move.forEach๋Š” reduce๋กœ accumulator์— answer ๋„ฃ์–ด์„œ ๊ฐ€๋Šฅํ• ๊ฒƒ ๊ฐ™๊ณ . + +borad[i][move] ์ด๊ฑฐ๋Š” ๊ณ ์ฐจํ•จ์ˆ˜๋กœ ๋ ๊นŒ? + +์™ธ๋ถ€์— flag ์„ ์–ธํ•ด๋†“๊ณ , ํ•œ๋ฒˆ 0์œผ๋กœ ๋ฐ”๋€Œ๋ฉด flag ๊ฐ’ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋ ๊ฑฐ๊ฐ™๊ธดํ•œ๋ฐ. ์˜๋ฏธ๊ฐ€ ์žˆ๋‚˜? for๋ฌธ์ด ๋” ๋น ๋ฅผ๊ฑฐ๊ฐ™์€๋ฐ. + +์†๋„ vs ๊ฐ€๋…์„ฑ? + +> ๊ตฌํ˜„ํ•ด ๋ณด๋‹ˆ ๊ฐ€๋…์„ฑ์ด ์ข‹์€๊ฒƒ ๊ฐ™์ง€๋„ ์•Š๋‹ค.. diff --git a/src/grap3fruit/js/programmers/64061_2.js b/src/grap3fruit/js/programmers/64061_2.js new file mode 100644 index 0000000..1ac2e7f --- /dev/null +++ b/src/grap3fruit/js/programmers/64061_2.js @@ -0,0 +1,40 @@ +const board = [ + [0, 0, 0, 0, 0], + [0, 0, 1, 0, 3], + [0, 2, 5, 0, 1], + [4, 2, 4, 4, 2], + [3, 5, 1, 3, 1], +]; +const moves = [1, 5, 3, 5, 1, 2, 1, 4]; + +const result = []; +let answer = 0; + +moves.forEach((move) => { + let changedFlag = false; + + board.forEach((el) => { + if (el[move - 1] !== 0 && changedFlag === false) { + if (result.length > 0) { + const prev = result.pop(); + + if (prev === el[move - 1]) { + el[move - 1] = 0; + answer += 2; + changedFlag = true; + } else { + result.push(prev); + } + } + if (changedFlag === false) { + result.push(el[move - 1]); + el[move - 1] = 0; + changedFlag = true; + } + } + }); +}); + +console.log(board); +console.log(result); +console.log(answer); diff --git a/src/grap3fruit/js/programmers/64065.js b/src/grap3fruit/js/programmers/64065.js new file mode 100644 index 0000000..e22cf06 --- /dev/null +++ b/src/grap3fruit/js/programmers/64065.js @@ -0,0 +1,58 @@ +const getSubtractedArr = (arrA, arrB) => arrB.filter((el) => !arrA.includes(el)); + +const getArrFromStr = (s) => { + const arr = s.split(''); + + let parent_arr = []; + let child_arr = []; + + for (let i = 1; i < arr.length - 1; i++) { + if (arr[i] === '}') { + parent_arr.push(child_arr); + child_arr = []; + } + + if (+arr[i] >= 0) { + if (+arr[i - 1] >= 0) { + const num = child_arr.pop(); + child_arr.push(Number(num + arr[i])); + } else { + child_arr.push(+arr[i]); + } + } + } + return parent_arr; +}; + +function solution(s) { + let answer = []; + const arr = getArrFromStr(s); + + for (let i = 0; i < arr.length; i++) { + for (let j = 0; j < arr.length; j++) { + if (arr[j].length === i + 1) { + const [temp] = getSubtractedArr(answer, arr[j]); + answer.push(temp); + break; + } + } + } + + return answer; +} + +// console.log(solution('{{2},{2,1},{2,1,3},{2,1,3,4}}')); +// console.log(solution('{{1,2,3},{2,1},{1,2,4,3},{2}}')); +// console.log(solution('{{20,111},{111}}')); +// console.log(solution('{{123}}')); +// console.log(solution('{{4,2,3},{3},{2,3,4,1},{2,3}}')); + +const setA = new Set(); +const setB = new Set(); +setA.add(1); +setA.add(2); +setB.add(3); +setB.add(2); + +console.log(setA, setB); +console.log(setA - setB); diff --git a/src/grap3fruit/js/programmers/64065.md b/src/grap3fruit/js/programmers/64065.md new file mode 100644 index 0000000..f4cf3d6 --- /dev/null +++ b/src/grap3fruit/js/programmers/64065.md @@ -0,0 +1,44 @@ +# 64065 2019 ์นด์นด์˜ค ๊ฒจ์šธ์ธํ„ด ํŠœํ”Œ - ์„ฑ๊ณต + +## ๊ตฌํ˜„ + +์ง‘ํ•ฉ ๊ธธ์ด์— ๋”ฐ๋ผ์„œ + +๊ฐ€์žฅ ์ž‘์€๊ฒŒ ๋งจ ์•ž์ด๊ณ  + +๊ทธ ๋‹ค์Œ ๊ธธ์ด์— ์ถ”๊ฐ€๋œ๊ฒŒ ๋‹ค์Œ๊บผ + +์ด๋Ÿฐ์‹์œผ๋กœ result ์ฑ„์šฐ๋ฉด ๋จ. + +python์— set ์“ฐ๋ฉด ์—„์ฒญ ์‰ฌ์› ์„๋“ฏ ใ…‹ใ…‹ + +--- + +1. {} ๋ฌธ์ž์—ด์„ [] ๋ฐฐ์—ด๋กœ ๋ฐ”๊พผ๋‹ค. + +``` +๋ถ€๋ชจ arr, ์ž์‹ arr๋ฅผ ๋‘”๋‹ค. +์ด์ฐจ์› ๋ฐฐ์—ด์˜ ๋ฐ”๊นฅ์„ ๋ถ€๋ชจ arr. +๋‚ด๋ถ€ el๋“ค์ด ์ž์‹ arr. + +์ „์ฒด string ๊ธธ์ด๋งŒํผ loop๋ฅผ ๋Œ๋ฉด์„œ + ๋งŒ์•ฝ '}' ๋ฉด + ๋ถ€๋ชจ์— ํ˜„์žฌ๊นŒ์ง€ ์ž์‹ arr ์ถ”๊ฐ€. + ์ž์‹ arr = [] + + '}' ๊ฐ€ ์•„๋‹๋–„ + ์•ž์ด ์ˆซ์ž๋ฉด ํ•ฉ์ณ์„œ ์ž์‹arr์— ๋„ฃ๊ณ  + ์•„๋‹ˆ๋ฉด ๊ทธ๋ƒฅ ๋„ฃ์Œ. + + +``` + +2. ๋ฐฐ์—ด ๊ฐฏ์ˆ˜๋งŒํผ ์ด์ค‘ํฌ๋ฌธ ๋ˆ๋‹ค. + +``` +for i = 1,2,3 ... + for j = 1,2,3 ... + ๋งŒ์•ฝ ํ˜„์žฌ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ i์ด๋ฉด, + answer์— ์ถ”๊ฐ€. + +``` diff --git a/src/grap3fruit/boj/1010.md b/src/grap3fruit/py/boj/1010.md similarity index 100% rename from src/grap3fruit/boj/1010.md rename to src/grap3fruit/py/boj/1010.md diff --git a/src/grap3fruit/boj/1010.py b/src/grap3fruit/py/boj/1010.py similarity index 100% rename from src/grap3fruit/boj/1010.py rename to src/grap3fruit/py/boj/1010.py diff --git a/src/grap3fruit/boj/1094.md b/src/grap3fruit/py/boj/1094.md similarity index 100% rename from src/grap3fruit/boj/1094.md rename to src/grap3fruit/py/boj/1094.md diff --git a/src/grap3fruit/boj/1094.py b/src/grap3fruit/py/boj/1094.py similarity index 100% rename from src/grap3fruit/boj/1094.py rename to src/grap3fruit/py/boj/1094.py diff --git a/src/grap3fruit/boj/11047.md b/src/grap3fruit/py/boj/11047.md similarity index 100% rename from src/grap3fruit/boj/11047.md rename to src/grap3fruit/py/boj/11047.md diff --git a/src/grap3fruit/boj/11047.py b/src/grap3fruit/py/boj/11047.py similarity index 100% rename from src/grap3fruit/boj/11047.py rename to src/grap3fruit/py/boj/11047.py diff --git a/src/grap3fruit/boj/11286.py b/src/grap3fruit/py/boj/11286.py similarity index 92% rename from src/grap3fruit/boj/11286.py rename to src/grap3fruit/py/boj/11286.py index 690cae3..35748d4 100644 --- a/src/grap3fruit/boj/11286.py +++ b/src/grap3fruit/py/boj/11286.py @@ -1,39 +1,39 @@ -import heapq -import sys - -# n = 18 -# x = [1, -# -1, -# 0, -# 0, -# 0, -# 1, -# 1, -# -1, -# -1, -# 2, -# -2, -# 0, -# 0, -# 0, -# 0, -# 0, -# 0, -# 0] - -n = int(sys.stdin.readline().strip()) -heap = [] -x = [] -for i in range(0, n): - x.append(int(sys.stdin.readline().strip())) - -for i in range(0,n): - if x[i] == 0: - if len(heap) == 0: - print(0) - continue - - print(heapq.heappop(heap)[1]) - continue - if x[i] != 0: - heapq.heappush(heap,(abs(x[i]),x[i])) +import heapq +import sys + +# n = 18 +# x = [1, +# -1, +# 0, +# 0, +# 0, +# 1, +# 1, +# -1, +# -1, +# 2, +# -2, +# 0, +# 0, +# 0, +# 0, +# 0, +# 0, +# 0] + +n = int(sys.stdin.readline().strip()) +heap = [] +x = [] +for i in range(0, n): + x.append(int(sys.stdin.readline().strip())) + +for i in range(0,n): + if x[i] == 0: + if len(heap) == 0: + print(0) + continue + + print(heapq.heappop(heap)[1]) + continue + if x[i] != 0: + heapq.heappush(heap,(abs(x[i]),x[i])) diff --git a/src/grap3fruit/boj/11399.md b/src/grap3fruit/py/boj/11399.md similarity index 100% rename from src/grap3fruit/boj/11399.md rename to src/grap3fruit/py/boj/11399.md diff --git a/src/grap3fruit/boj/11399.py b/src/grap3fruit/py/boj/11399.py similarity index 100% rename from src/grap3fruit/boj/11399.py rename to src/grap3fruit/py/boj/11399.py diff --git a/src/grap3fruit/boj/1149.md b/src/grap3fruit/py/boj/1149.md similarity index 100% rename from src/grap3fruit/boj/1149.md rename to src/grap3fruit/py/boj/1149.md diff --git a/src/grap3fruit/boj/1149.py b/src/grap3fruit/py/boj/1149.py similarity index 100% rename from src/grap3fruit/boj/1149.py rename to src/grap3fruit/py/boj/1149.py diff --git a/src/grap3fruit/boj/11497.md b/src/grap3fruit/py/boj/11497.md similarity index 100% rename from src/grap3fruit/boj/11497.md rename to src/grap3fruit/py/boj/11497.md diff --git a/src/grap3fruit/boj/11497.py b/src/grap3fruit/py/boj/11497.py similarity index 100% rename from src/grap3fruit/boj/11497.py rename to src/grap3fruit/py/boj/11497.py diff --git a/src/grap3fruit/boj/11727.md b/src/grap3fruit/py/boj/11727.md similarity index 100% rename from src/grap3fruit/boj/11727.md rename to src/grap3fruit/py/boj/11727.md diff --git a/src/grap3fruit/boj/11727.py b/src/grap3fruit/py/boj/11727.py similarity index 100% rename from src/grap3fruit/boj/11727.py rename to src/grap3fruit/py/boj/11727.py diff --git a/src/grap3fruit/boj/1254.md b/src/grap3fruit/py/boj/1254.md similarity index 100% rename from src/grap3fruit/boj/1254.md rename to src/grap3fruit/py/boj/1254.md diff --git a/src/grap3fruit/boj/1254.py b/src/grap3fruit/py/boj/1254.py similarity index 100% rename from src/grap3fruit/boj/1254.py rename to src/grap3fruit/py/boj/1254.py diff --git a/src/grap3fruit/boj/12731.md b/src/grap3fruit/py/boj/12731.md similarity index 100% rename from src/grap3fruit/boj/12731.md rename to src/grap3fruit/py/boj/12731.md diff --git a/src/grap3fruit/boj/12731.py b/src/grap3fruit/py/boj/12731.py similarity index 100% rename from src/grap3fruit/boj/12731.py rename to src/grap3fruit/py/boj/12731.py diff --git a/src/grap3fruit/boj/1316.py b/src/grap3fruit/py/boj/1316.py similarity index 100% rename from src/grap3fruit/boj/1316.py rename to src/grap3fruit/py/boj/1316.py diff --git a/src/grap3fruit/boj/14889.md b/src/grap3fruit/py/boj/14889.md similarity index 100% rename from src/grap3fruit/boj/14889.md rename to src/grap3fruit/py/boj/14889.md diff --git a/src/grap3fruit/boj/14889.py b/src/grap3fruit/py/boj/14889.py similarity index 100% rename from src/grap3fruit/boj/14889.py rename to src/grap3fruit/py/boj/14889.py diff --git a/src/grap3fruit/boj/1697.md b/src/grap3fruit/py/boj/1697.md similarity index 100% rename from src/grap3fruit/boj/1697.md rename to src/grap3fruit/py/boj/1697.md diff --git a/src/grap3fruit/boj/1697.py b/src/grap3fruit/py/boj/1697.py similarity index 100% rename from src/grap3fruit/boj/1697.py rename to src/grap3fruit/py/boj/1697.py diff --git a/src/grap3fruit/boj/17609.md b/src/grap3fruit/py/boj/17609.md similarity index 100% rename from src/grap3fruit/boj/17609.md rename to src/grap3fruit/py/boj/17609.md diff --git a/src/grap3fruit/boj/17609.py b/src/grap3fruit/py/boj/17609.py similarity index 100% rename from src/grap3fruit/boj/17609.py rename to src/grap3fruit/py/boj/17609.py diff --git a/src/grap3fruit/boj/18233.md b/src/grap3fruit/py/boj/18233.md similarity index 100% rename from src/grap3fruit/boj/18233.md rename to src/grap3fruit/py/boj/18233.md diff --git a/src/grap3fruit/boj/18233.py b/src/grap3fruit/py/boj/18233.py similarity index 100% rename from src/grap3fruit/boj/18233.py rename to src/grap3fruit/py/boj/18233.py diff --git a/src/grap3fruit/boj/19238.py b/src/grap3fruit/py/boj/19238.py similarity index 100% rename from src/grap3fruit/boj/19238.py rename to src/grap3fruit/py/boj/19238.py diff --git a/src/grap3fruit/boj/1932.md b/src/grap3fruit/py/boj/1932.md similarity index 100% rename from src/grap3fruit/boj/1932.md rename to src/grap3fruit/py/boj/1932.md diff --git a/src/grap3fruit/boj/1932.py b/src/grap3fruit/py/boj/1932.py similarity index 100% rename from src/grap3fruit/boj/1932.py rename to src/grap3fruit/py/boj/1932.py diff --git a/src/grap3fruit/boj/1946.md b/src/grap3fruit/py/boj/1946.md similarity index 100% rename from src/grap3fruit/boj/1946.md rename to src/grap3fruit/py/boj/1946.md diff --git a/src/grap3fruit/boj/1946.py b/src/grap3fruit/py/boj/1946.py similarity index 100% rename from src/grap3fruit/boj/1946.py rename to src/grap3fruit/py/boj/1946.py diff --git a/src/grap3fruit/boj/1991.py b/src/grap3fruit/py/boj/1991.py similarity index 100% rename from src/grap3fruit/boj/1991.py rename to src/grap3fruit/py/boj/1991.py diff --git a/src/grap3fruit/py/boj/2011.md b/src/grap3fruit/py/boj/2011.md new file mode 100644 index 0000000..bf0d914 --- /dev/null +++ b/src/grap3fruit/py/boj/2011.md @@ -0,0 +1,18 @@ +# 2011 - ํžŒํŠธ + +## DP + +์ฒ˜์Œ์—” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์„œ, + +2๊ฐœ์”ฉ ๋ฌถ์ด๋Š”๊ฒŒ 0 0 and int(input[0]) < 3: + dp[1] = 1 + else: + return 0 + + if int(input[1]) != 0: # input[1]์ด 0์ด ์•„๋‹ˆ๊ณ , + if (int(input[1]) < 7 and int(input[0]) == 2) or int(input[0]) == 1: # 10 ~ 26 ์‚ฌ์ด๋ฉด + dp[1] = 2 + else: + dp[1] = 1 + + if len(input) == 2: # ์ž…๋ ฅ์ด ๋‘๊ฐœ๋ฉด return + return dp[1] + + n = 2 + while n < len(input): # dp[2] ๋ถ€ํ„ฐ ์—ฌ๊ธฐ์„œ ๊ตฌํ•จ. + if int(input[n]) > 0: # input[n]์ด 0๋ณด๋‹ค ํด ๊ฒฝ์šฐ๋Š” input[n-1]์— ๊ทธ๋Œ€๋กœ ๋ถ™์—ฌ์ค„ ์ˆ˜ ์žˆ์Œ. + dp[n] = dp[n-1] + + # n,n-1์ด 10 ~ 26 ์‚ฌ์ด๋ฉด, dp[n-2]์— 10~26์„ ๋ถ™์ผ ์ˆ˜ ์žˆ์Œ. + if (int(input[n]) < 7 and int(input[n-1]) == 2) or int(input[n-1]) == 1: + dp[n] = (dp[n-2] + dp[n]) + + n += 1 + + return(dp[len(input)-1] % 1000000) + + +print(calc(input, dp)) diff --git a/src/grap3fruit/boj/2178.md b/src/grap3fruit/py/boj/2178.md similarity index 100% rename from src/grap3fruit/boj/2178.md rename to src/grap3fruit/py/boj/2178.md diff --git a/src/grap3fruit/boj/2178.py b/src/grap3fruit/py/boj/2178.py similarity index 100% rename from src/grap3fruit/boj/2178.py rename to src/grap3fruit/py/boj/2178.py diff --git a/src/grap3fruit/boj/2502.md b/src/grap3fruit/py/boj/2502.md similarity index 79% rename from src/grap3fruit/boj/2502.md rename to src/grap3fruit/py/boj/2502.md index a0ee18a..3d9e872 100644 --- a/src/grap3fruit/boj/2502.md +++ b/src/grap3fruit/py/boj/2502.md @@ -11,6 +11,6 @@ dp[2] = 'b' dp[K] = 'bababbab' ์ด๋Ÿฐ์‹์œผ๋กœ ๋‚˜์˜จ๋‹ค. ์ด๊ฑธ -count_a _ j + count_b _ i == K ์‹์œผ๋กœ ๋ฐ”๊ฟ”์„œ +count_a x j + count_b x i == K ์‹์œผ๋กœ ๋ฐ”๊ฟ”์„œ i, j ์ด์ค‘ํฌ๋ฌธ ๋Œ๋ ค์„œ ๊ตฌํ•จ diff --git a/src/grap3fruit/boj/2502.py b/src/grap3fruit/py/boj/2502.py similarity index 100% rename from src/grap3fruit/boj/2502.py rename to src/grap3fruit/py/boj/2502.py diff --git a/src/grap3fruit/boj/2579.md b/src/grap3fruit/py/boj/2579.md similarity index 100% rename from src/grap3fruit/boj/2579.md rename to src/grap3fruit/py/boj/2579.md diff --git a/src/grap3fruit/boj/2579.py b/src/grap3fruit/py/boj/2579.py similarity index 100% rename from src/grap3fruit/boj/2579.py rename to src/grap3fruit/py/boj/2579.py diff --git a/src/grap3fruit/boj/2606.md b/src/grap3fruit/py/boj/2606.md similarity index 100% rename from src/grap3fruit/boj/2606.md rename to src/grap3fruit/py/boj/2606.md diff --git a/src/grap3fruit/boj/2606.py b/src/grap3fruit/py/boj/2606.py similarity index 100% rename from src/grap3fruit/boj/2606.py rename to src/grap3fruit/py/boj/2606.py diff --git a/src/grap3fruit/boj/2644.md b/src/grap3fruit/py/boj/2644.md similarity index 100% rename from src/grap3fruit/boj/2644.md rename to src/grap3fruit/py/boj/2644.md diff --git a/src/grap3fruit/boj/2644.py b/src/grap3fruit/py/boj/2644.py similarity index 90% rename from src/grap3fruit/boj/2644.py rename to src/grap3fruit/py/boj/2644.py index b391fde..353c60d 100644 --- a/src/grap3fruit/boj/2644.py +++ b/src/grap3fruit/py/boj/2644.py @@ -41,9 +41,6 @@ def solution(N, A, B, M, dic): if (A in result_A) and (B in result_B): intersec = result_A & result_B - # if len(result_A - intersec) == 1 and len(result_B - intersec) == 1: - # return 1 - return abs(len(result_A - intersec) + len(result_B - intersec)) return -1 diff --git a/src/grap3fruit/boj/2957.py b/src/grap3fruit/py/boj/2957.py similarity index 100% rename from src/grap3fruit/boj/2957.py rename to src/grap3fruit/py/boj/2957.py diff --git a/src/grap3fruit/boj/7569.md b/src/grap3fruit/py/boj/7569.md similarity index 100% rename from src/grap3fruit/boj/7569.md rename to src/grap3fruit/py/boj/7569.md diff --git a/src/grap3fruit/boj/7569.py b/src/grap3fruit/py/boj/7569.py similarity index 100% rename from src/grap3fruit/boj/7569.py rename to src/grap3fruit/py/boj/7569.py diff --git a/src/grap3fruit/boj/9095.md b/src/grap3fruit/py/boj/9095.md similarity index 100% rename from src/grap3fruit/boj/9095.md rename to src/grap3fruit/py/boj/9095.md diff --git a/src/grap3fruit/boj/9095.py b/src/grap3fruit/py/boj/9095.py similarity index 100% rename from src/grap3fruit/boj/9095.py rename to src/grap3fruit/py/boj/9095.py diff --git a/src/grap3fruit/input_test.py b/src/grap3fruit/py/input_test.py similarity index 100% rename from src/grap3fruit/input_test.py rename to src/grap3fruit/py/input_test.py diff --git a/src/grap3fruit/programmers/17685.py b/src/grap3fruit/py/programmers/17685.py similarity index 100% rename from src/grap3fruit/programmers/17685.py rename to src/grap3fruit/py/programmers/17685.py diff --git a/src/grap3fruit/programmers/42578.py b/src/grap3fruit/py/programmers/42578.py similarity index 100% rename from src/grap3fruit/programmers/42578.py rename to src/grap3fruit/py/programmers/42578.py diff --git a/src/grap3fruit/programmers/60057.py b/src/grap3fruit/py/programmers/60057.py similarity index 100% rename from src/grap3fruit/programmers/60057.py rename to src/grap3fruit/py/programmers/60057.py diff --git "a/src/kyu9341/boj2178_\353\257\270\353\241\234\355\203\220\354\203\211/2178_\353\257\270\353\241\234\355\203\220\354\203\211.cpp" "b/src/kyu9341/boj2178_\353\257\270\353\241\234\355\203\220\354\203\211/2178_\353\257\270\353\241\234\355\203\220\354\203\211.cpp" new file mode 100644 index 0000000..35732a0 --- /dev/null +++ "b/src/kyu9341/boj2178_\353\257\270\353\241\234\355\203\220\354\203\211/2178_\353\257\270\353\241\234\355\203\220\354\203\211.cpp" @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const int MAX = 100; +int n, m; +int dx[] = { 0, 0, 1, -1 }; +int dy[] = { 1, -1, 0, 0 }; +int map[MAX + 1][MAX + 1]; +int dist[MAX + 1][MAX + 1]; +queue< pair > q; + +void bfs(int sx, int sy) { + q.push(make_pair(sx, sy)); + dist[sx][sy] = 1; + + while(!q.empty()) { + int x = q.front().first; + int y = q.front().second; + q.pop(); + + for (int i = 0; i < 4; i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + + if (!(nx >= 0 && ny >= 0 && nx < n && ny < m)) continue; + if (dist[nx][ny] != -1 || map[nx][ny] != 1) continue; + q.push(make_pair(nx, ny)); + dist[nx][ny] = dist[x][y] + 1; + } + + if (dist[n - 1][m - 1] != -1) break; + } +} + +int main() { + + cin >> n >> m; + + memset(dist, -1, sizeof(dist)); + + for (int i = 0; i < n; i++) + for (int j = 0; j < m; j++) + scanf("%1d", &map[i][j]); + + bfs(0, 0); + + cout << dist[n - 1][m - 1] << '\n'; +} + +// C++ ์‚ฌ์šฉ์ž๋“ค ์ค‘์— ์ˆซ์ž ํ•˜๋‚˜์”ฉ์„ ๋–ผ์–ด์„œ ์ž…๋ ฅ๋ฐ›๊ณ  ์‹ถ์–ด์„œ cin์„ ์“ฐ๋‹ค๊ฐ€ ๋ฏธ๋กœ๋งŒ scanf๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋•Œ์—๋Š” ์ ˆ๋Œ€๋กœ sync_with_stdio(false); ๋ฅผ ํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. diff --git "a/src/kyu9341/boj2502_\353\226\241\353\250\271\353\212\224\355\230\270\353\236\221\354\235\264/2502_\353\226\241\353\250\271\353\212\224\355\230\270\353\236\221\354\235\264.cpp" "b/src/kyu9341/boj2502_\353\226\241\353\250\271\353\212\224\355\230\270\353\236\221\354\235\264/2502_\353\226\241\353\250\271\353\212\224\355\230\270\353\236\221\354\235\264.cpp" new file mode 100644 index 0000000..787b265 --- /dev/null +++ "b/src/kyu9341/boj2502_\353\226\241\353\250\271\353\212\224\355\230\270\353\236\221\354\235\264/2502_\353\226\241\353\250\271\353\212\224\355\230\270\353\236\221\354\235\264.cpp" @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; + +int day, cake; + +int main() { + int A, B; + + cin >> day >> cake; + vector< pair > d(day + 1); + + d[1] = make_pair(1, 0); + d[2] = make_pair(0, 1); + + for (int i = 3; i <= day; i++) { + d[i].first = d[i - 1].first + d[i - 2].first; + d[i].second = d[i - 1].second + d[i - 2].second; + } + + for (int i = 1; i <= cake; i++) { + int cntA = d[day].first; + int cntB = d[day].second; + + if ((cake - cntA * i) % cntB == 0) { + A = i; + B = (cake - cntA * A) / cntB; + break; + } + } + + cout << A << '\n' << B << '\n'; +} + + diff --git "a/src/kyu9341/boj2644_\354\264\214\354\210\230\352\263\204\354\202\260/2644_\354\264\214\354\210\230\352\263\204\354\202\260.cpp" "b/src/kyu9341/boj2644_\354\264\214\354\210\230\352\263\204\354\202\260/2644_\354\264\214\354\210\230\352\263\204\354\202\260.cpp" new file mode 100644 index 0000000..6f91eec --- /dev/null +++ "b/src/kyu9341/boj2644_\354\264\214\354\210\230\352\263\204\354\202\260/2644_\354\264\214\354\210\230\352\263\204\354\202\260.cpp" @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const int MAX = 100; +int n, m; +vector v[MAX + 1]; +int dist[MAX + 1]; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + int x, y; + + cin >> n >> x >> y >> m; + + memset(dist, -1, sizeof(dist)); + int parent, child; + + while (m--) { + cin >> parent >> child; + v[parent].push_back(child); + v[child].push_back(parent); + } + + queue q; + dist[x] = 0; + q.push(x); + + while(!q.empty()) { + int cur = q.front(); + q.pop(); + + for (int i = 0; i < v[cur].size(); i++) { + int next = v[cur][i]; + if (dist[next] != -1) continue; + dist[next] = dist[cur] + 1; + q.push(next); + } + + if (dist[y] != -1) break; + } + + cout << dist[y] << '\n'; +} + diff --git "a/src/kyu9341/programmers_\352\260\200\354\236\245\352\270\264\355\214\260\353\246\260\353\223\234\353\241\254/index.js" "b/src/kyu9341/programmers_\352\260\200\354\236\245\352\270\264\355\214\260\353\246\260\353\223\234\353\241\254/index.js" new file mode 100644 index 0000000..684466e --- /dev/null +++ "b/src/kyu9341/programmers_\352\260\200\354\236\245\352\270\264\355\214\260\353\246\260\353\223\234\353\241\254/index.js" @@ -0,0 +1,35 @@ +const getLongestPalindrome = (left, right, arrFromStr) => { + let maxLength = 0; + const isInRange = (left, right) => left >= 0 && right < arrFromStr.length; + + while (isInRange(left, right)) { + if (arrFromStr[left] !== arrFromStr[right]) return maxLength; + + maxLength = right - left + 1; + left -= 1; + right += 1; + } + + return maxLength; +}; + +const solution = s => { + let maxLength = 0; + const arrFromStr = [...s]; + + const checkLength = (_, index) => { + const odd = getLongestPalindrome(index, index, arrFromStr); + const even = getLongestPalindrome(index, index + 1, arrFromStr); + maxLength = Math.max(maxLength, Math.max(odd, even)); + }; + + arrFromStr.forEach(checkLength); + + return maxLength; +}; + +(() => { + const inputs = ['abcdcba', 'abacde']; + + inputs.forEach(input => console.log(solution(input))); +})(); 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))); +})(); diff --git "a/src/kyu9341/programmers_\355\201\254\353\240\210\354\235\270\354\235\270\355\230\225\353\275\221\352\270\260\352\262\214\354\236\204/index.js" "b/src/kyu9341/programmers_\355\201\254\353\240\210\354\235\270\354\235\270\355\230\225\353\275\221\352\270\260\352\262\214\354\236\204/index.js" new file mode 100644 index 0000000..0734bcd --- /dev/null +++ "b/src/kyu9341/programmers_\355\201\254\353\240\210\354\235\270\354\235\270\355\230\225\353\275\221\352\270\260\352\262\214\354\236\204/index.js" @@ -0,0 +1,38 @@ +const range = (start, end) => + start < end ? Array.from({ length: end - start + 1 }, (_, idx) => idx + start) : []; + +const solution = (board, moves) => { + let count = 0; + const stack = []; + + const pickDoll = col => { + for (const row of range(0, board.length - 1)) { + const currentDoll = board[row][col - 1]; + board[row][col - 1] = 0; + + const isExist = currentDoll !== 0; + const isEqualToPrevDoll = stack[stack.length - 1] === currentDoll; + + if (isExist) { + if (isEqualToPrevDoll) { + stack.pop(); + count += 2; + break; + } + stack.push(currentDoll); + break; + } + } + }; + + moves.forEach(pickDoll); + return count; +} + +(() => { + const board = [[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]]; + const moves = [1,5,3,5,1,2,1,4]; + + console.log(solution(board, moves)); +})(); + diff --git "a/src/kyu9341/programmers_\355\212\234\355\224\214/index.js" "b/src/kyu9341/programmers_\355\212\234\355\224\214/index.js" new file mode 100644 index 0000000..ec57b1a --- /dev/null +++ "b/src/kyu9341/programmers_\355\212\234\355\224\214/index.js" @@ -0,0 +1,26 @@ +const parseStringToArray = str => str.substring(2, str.length - 2) + .split("},{") + .map(el => el.split(',').map(str => Number(str))); + +const solution = s => { + const set = new Set(); + const arr = parseStringToArray(s); + + const compareLength = (prev, cur) => prev.length - cur.length; + arr.sort(compareLength).flat().forEach(el => set.add(el));; + + return [...set]; +} + +(() => { + const inputs = [ + "{{2},{2,1},{2,1,3},{2,1,3,4}}", + "{{1,2,3},{2,1},{1,2,4,3},{2}}", + "{{20,111},{111}}", + "{{123}}", + "{{4,2,3},{3},{2,3,4,1},{2,3}}", + ]; + + inputs.forEach(input => console.log(solution(input))); +})(); + diff --git a/src/kyu9341/queue/queue.js b/src/kyu9341/queue/queue.js new file mode 100644 index 0000000..1a0747e --- /dev/null +++ b/src/kyu9341/queue/queue.js @@ -0,0 +1,53 @@ +class Node { + constructor(data) { + this.data = data; + this.next = null; + } +} + +class Queue { + constructor() { + this.head = null; + this.tail = null; + this.size = 0; + } + + push(data) { + const node = new Node(data); + if (this.isEmpty()) { + this.head = node; + this.head.next = this.tail; + } else this.tail.next = node; + + this.tail = node; + this.size += 1; + } + + pop() { + if (this.isEmpty()) return null; + if (this.head === this.tail) this.tail = null; + + const headData = this.head.data; + this.head = this.head.next; + this.size -= 1; + + return headData; + } + + isEmpty() { + return this.size === 0; + } + + getArray() { + const array = []; + if (this.isEmpty()) return array; + + let currentNode = this.head; + while (currentNode) { + array.push(currentNode.data); + currentNode = currentNode.next; + } + + return array; + } +}