From d5ef0ce17c800d473c05b6ec0523b5a10633bf4f Mon Sep 17 00:00:00 2001 From: krokerdile Date: Tue, 27 May 2025 11:18:25 +0900 Subject: [PATCH 1/9] linked-list-cycle solution --- linked-list-cycle/krokerdile.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 linked-list-cycle/krokerdile.js diff --git a/linked-list-cycle/krokerdile.js b/linked-list-cycle/krokerdile.js new file mode 100644 index 000000000..55f7a6600 --- /dev/null +++ b/linked-list-cycle/krokerdile.js @@ -0,0 +1,23 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function(head) { + let list = new Set(); + while (head != null) { + if (list.has(head)) + return true; + list.add(head); + head = head.next; + } + + return false; +}; \ No newline at end of file From de2da781cf792525b601eb115904b40ec3380896 Mon Sep 17 00:00:00 2001 From: krokerdile Date: Tue, 27 May 2025 11:21:58 +0900 Subject: [PATCH 2/9] =?UTF-8?q?=EA=B0=9C=ED=96=89=EB=AC=B8=EC=9E=90=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- linked-list-cycle/krokerdile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linked-list-cycle/krokerdile.js b/linked-list-cycle/krokerdile.js index 55f7a6600..8e7fc3148 100644 --- a/linked-list-cycle/krokerdile.js +++ b/linked-list-cycle/krokerdile.js @@ -20,4 +20,4 @@ var hasCycle = function(head) { } return false; -}; \ No newline at end of file +}; From 194eb6cd904abaa94631fd39f25a9c2c07a8ddad Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 30 May 2025 16:18:40 +0900 Subject: [PATCH 3/9] palindromic-substrings solution --- palindromic-substrings/krokerdile.js | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 palindromic-substrings/krokerdile.js diff --git a/palindromic-substrings/krokerdile.js b/palindromic-substrings/krokerdile.js new file mode 100644 index 000000000..d5aa62516 --- /dev/null +++ b/palindromic-substrings/krokerdile.js @@ -0,0 +1,37 @@ +/** + * @param {string} s + * @return {number} + * + * 시간복잡도: O(n^2) + * - 각 문자(i)를 중심으로 확장 → 최대 n번 + * - 각 중심에서 양쪽으로 확장하면서 비교 → 최대 n번 + * → O(n) * O(n) = O(n^2) + * + * 공간복잡도: O(1) + * - 추가 공간 없이 count 변수와 index만 사용 + * - 문자열을 복사하거나 메모이제이션 하지 않음 + */ +var countSubstrings = function(s) { + let count = 0; + + // 전체 문자열을 순회하면서 각 위치를 팰린드롬 중심으로 잡음 + for (let i = 0; i < s.length; i++) { + // 홀수 길이 팰린드롬: ex. "aba" + helper(i, i); + + // 짝수 길이 팰린드롬: ex. "abba" + helper(i, i + 1); + } + + // 팰린드롬이 유지되는 동안 count 증가시키는 함수 + function helper(left, right) { + // 조건이 맞을 때마다 좌우로 확장 + while (left >= 0 && right < s.length && s[left] === s[right]) { + count++; // 유효한 팰린드롬 하나 발견 + left--; + right++; + } + } + + return count; +}; \ No newline at end of file From 26c990d4ab661aef7c89d30a22addcf76d349916 Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 30 May 2025 16:31:50 +0900 Subject: [PATCH 4/9] =?UTF-8?q?=EA=B0=9C=ED=96=89=20=EB=AC=B8=EC=9E=90=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- palindromic-substrings/krokerdile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/palindromic-substrings/krokerdile.js b/palindromic-substrings/krokerdile.js index d5aa62516..ea73732fb 100644 --- a/palindromic-substrings/krokerdile.js +++ b/palindromic-substrings/krokerdile.js @@ -34,4 +34,4 @@ var countSubstrings = function(s) { } return count; -}; \ No newline at end of file +}; From 2a99ebeb338c92fa85ff53de48f3e21e0b470163 Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 30 May 2025 16:31:57 +0900 Subject: [PATCH 5/9] lcs solution --- longest-common-subsequence/krokerdile.js | 63 ++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 longest-common-subsequence/krokerdile.js diff --git a/longest-common-subsequence/krokerdile.js b/longest-common-subsequence/krokerdile.js new file mode 100644 index 000000000..066c345de --- /dev/null +++ b/longest-common-subsequence/krokerdile.js @@ -0,0 +1,63 @@ +/** + * @param {string} text1 + * @param {string} text2 + * @return {number} + * + * 시간복잡도: O(m * n) + * - m = text1.length, n = text2.length + * - 2중 for문으로 dp 테이블 채우기 + * + * 공간복잡도: O(m * n) + * - dp 배열에 m * n 만큼의 공간 사용 + * - 최적화하려면 배열 2개로도 가능 + */ +var longestCommonSubsequence = function(text1, text2) { + let m = text1.length; + let n = text2.length; + + // 2차원 dp 배열 초기화 (m+1) x (n+1) + const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + + // dp[i][j] = text1[0..i-1], text2[0..j-1] 까지의 LCS 길이 + for (let i = 1; i <= m; i++) { + for (let j = 1; j <= n; j++) { + if (text1[i - 1] === text2[j - 1]) { + // 문자가 같으면, 이전까지의 LCS + 1 + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + // 아니면, 이전 행/열 중 더 큰 값 선택 + dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + + return dp[m][n]; // 마지막 cell이 정답 +}; + +//실패한 dfs 코드 +/** + * @param {string} text1 + * @param {string} text2 + * @return {number} + */ +var longestCommonSubsequence = function(text1, text2) { + function dfs(i, j) { + // 종료 조건: 인덱스 범위를 벗어나면 0 + if (i === text1.length || j === text2.length) { + return 0; + } + + // 문자가 같으면, 다음 문자로 이동하며 길이 +1 + if (text1[i] === text2[j]) { + return 1 + dfs(i + 1, j + 1); + } + + // 문자가 다르면, 두 가지 갈래 중 최댓값을 선택 + return Math.max( + dfs(i + 1, j), // text1의 다음 문자로 + dfs(i, j + 1) // text2의 다음 문자로 + ); + } + + return dfs(0, 0); +}; From e7a30449950c8ad4d3ebff1c18cba97894ac67a9 Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 30 May 2025 16:36:20 +0900 Subject: [PATCH 6/9] sum-of-two-integers --- sum-of-two-integers/krokerdile.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 sum-of-two-integers/krokerdile.js diff --git a/sum-of-two-integers/krokerdile.js b/sum-of-two-integers/krokerdile.js new file mode 100644 index 000000000..d6d1faa96 --- /dev/null +++ b/sum-of-two-integers/krokerdile.js @@ -0,0 +1,16 @@ +/** + * @param {number} a + * @param {number} b + * @return {number} + */ +function getSum(a, b) { + while (b !== 0) { + const sum = a ^ b; // 자리올림 없는 합 + const carry = (a & b) << 1; // 자리올림 비트 + + a = sum; + b = carry; + } + + return a; +} \ No newline at end of file From dd4831a4040525776084d45f541842cde3405bd2 Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 30 May 2025 16:40:52 +0900 Subject: [PATCH 7/9] =?UTF-8?q?=EA=B0=9C=ED=96=89=20=EB=AC=B8=EC=9E=90=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sum-of-two-integers/krokerdile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sum-of-two-integers/krokerdile.js b/sum-of-two-integers/krokerdile.js index d6d1faa96..089327266 100644 --- a/sum-of-two-integers/krokerdile.js +++ b/sum-of-two-integers/krokerdile.js @@ -13,4 +13,4 @@ function getSum(a, b) { } return a; -} \ No newline at end of file +} From b941159ff5db0c2a55454076232b5d4fa9a9b8de Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 30 May 2025 16:47:17 +0900 Subject: [PATCH 8/9] clone graph solution --- clone-graph/krokerdile.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 clone-graph/krokerdile.js diff --git a/clone-graph/krokerdile.js b/clone-graph/krokerdile.js new file mode 100644 index 000000000..515d8a794 --- /dev/null +++ b/clone-graph/krokerdile.js @@ -0,0 +1,26 @@ +/** + * @param {_Node} node + * @return {_Node} + */ +var cloneGraph = function(node) { + if (!node) return null; + + const visited = new Map(); // 원본 노드 -> 복제 노드 + + function dfs(curr) { + if (visited.has(curr)) { + return visited.get(curr); // 이미 복제한 경우 + } + + const copy = new _Node(curr.val); + visited.set(curr, copy); // 복제한 노드 저장 + + for (const neighbor of curr.neighbors) { + copy.neighbors.push(dfs(neighbor)); // 이웃도 재귀적으로 복사 + } + + return copy; + } + + return dfs(node); +}; From 8705c4e325ae208c5d345eff1efb21f751fa5c6f Mon Sep 17 00:00:00 2001 From: krokerdile Date: Fri, 30 May 2025 16:47:26 +0900 Subject: [PATCH 9/9] =?UTF-8?q?=EA=B0=9C=ED=96=89=EB=AC=B8=EC=9E=90=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clone-graph/krokerdile.js | 1 + 1 file changed, 1 insertion(+) diff --git a/clone-graph/krokerdile.js b/clone-graph/krokerdile.js index 515d8a794..aaa71e400 100644 --- a/clone-graph/krokerdile.js +++ b/clone-graph/krokerdile.js @@ -24,3 +24,4 @@ var cloneGraph = function(node) { return dfs(node); }; +