From 4213391fc439ce9f4790e30d096f03e74939b636 Mon Sep 17 00:00:00 2001 From: Paik Date: Sun, 29 Dec 2024 14:04:02 +0900 Subject: [PATCH 01/10] feat: 21. Merge Two Sorted Lists --- merge-two-sorted-lists/gwbaik9717.js | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 merge-two-sorted-lists/gwbaik9717.js diff --git a/merge-two-sorted-lists/gwbaik9717.js b/merge-two-sorted-lists/gwbaik9717.js new file mode 100644 index 000000000..513778a83 --- /dev/null +++ b/merge-two-sorted-lists/gwbaik9717.js @@ -0,0 +1,46 @@ +// m: list1, n: list2 +// Time complexity: O(m+n) +// Space complexity: O(m+n) + +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} list1 + * @param {ListNode} list2 + * @return {ListNode} + */ +var mergeTwoLists = function (list1, list2) { + const answer = new ListNode(); + let current = answer; + + while (list1 && list2) { + if (list1.val < list2.val) { + current.next = new ListNode(list1.val); + list1 = list1.next; + } else { + current.next = new ListNode(list2.val); + list2 = list2.next; + } + + current = current.next; + } + + while (list1) { + current.next = new ListNode(list1.val); + list1 = list1.next; + current = current.next; + } + + while (list2) { + current.next = new ListNode(list2.val); + list2 = list2.next; + current = current.next; + } + + return answer.next; +}; From e97aff4859fff20938b46fb1c8a31e39922be7df Mon Sep 17 00:00:00 2001 From: Paik Date: Mon, 30 Dec 2024 08:03:38 +0900 Subject: [PATCH 02/10] feat: 268. Missing Number --- missing-number/gwbaik9717.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 missing-number/gwbaik9717.js diff --git a/missing-number/gwbaik9717.js b/missing-number/gwbaik9717.js new file mode 100644 index 000000000..5df34e02c --- /dev/null +++ b/missing-number/gwbaik9717.js @@ -0,0 +1,15 @@ +// Time complexity: O(n) +// Space complexity: O(1) + +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function (nums) { + const n = nums.length; + const target = (n * (n + 1)) / 2; + + const sum = nums.reduce((a, c) => a + c, 0); + + return target - sum; +}; From 43853fcc2ea1758dfd57c1a9e03fcde02e73ee90 Mon Sep 17 00:00:00 2001 From: Paik Date: Mon, 30 Dec 2024 08:53:55 +0900 Subject: [PATCH 03/10] feat: 79. Word Search --- word-search/gwbaik9717.js | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 word-search/gwbaik9717.js diff --git a/word-search/gwbaik9717.js b/word-search/gwbaik9717.js new file mode 100644 index 000000000..182641028 --- /dev/null +++ b/word-search/gwbaik9717.js @@ -0,0 +1,62 @@ +// h: height of the board, w: width of the board, n: length of the word +// Time complexity: O(h * w * 4**n) +// Space complexity: O(h * w + n) + +/** + * @param {character[][]} board + * @param {string} word + * @return {boolean} + */ +var exist = function (board, word) { + const n = word.length; + const h = board.length; + const w = board[0].length; + + const dy = [1, 0, -1, 0]; + const dx = [0, 1, 0, -1]; + + const checked = Array.from({ length: h }, () => + Array.from({ length: w }, () => 0) + ); + + let answer = false; + + const dfs = (current, index) => { + if (index === n - 1) { + answer = true; + return; + } + + const [cy, cx] = current; + checked[cy][cx] = 1; + + for (let i = 0; i < dy.length; i++) { + const ny = cy + dy[i]; + const nx = cx + dx[i]; + const ni = index + 1; + + if ( + ny >= 0 && + ny < h && + nx >= 0 && + nx < w && + checked[ny][nx] === 0 && + word[ni] === board[ny][nx] + ) { + dfs([ny, nx], ni); + } + } + + checked[cy][cx] = 0; + }; + + for (let i = 0; i < h; i++) { + for (let j = 0; j < w; j++) { + if (board[i][j] === word[0] && !answer) { + dfs([i, j], 0); + } + } + } + + return answer; +}; From 32c4b5660ac831414aab9acf62bb63d4a4f0f33a Mon Sep 17 00:00:00 2001 From: Paik Date: Wed, 1 Jan 2025 10:09:09 +0900 Subject: [PATCH 04/10] feat: 647. Palindromic Substrings --- palindromic-substrings/gwbaik9717.js | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 palindromic-substrings/gwbaik9717.js diff --git a/palindromic-substrings/gwbaik9717.js b/palindromic-substrings/gwbaik9717.js new file mode 100644 index 000000000..6d65d5e8b --- /dev/null +++ b/palindromic-substrings/gwbaik9717.js @@ -0,0 +1,46 @@ +// Time complexity: O(n^2) +// Space complexity: O(n^2) + +/** + * @param {string} s + * @return {number} + */ +var countSubstrings = function (s) { + const n = s.length; + const dp = Array.from({ length: n }, () => + Array.from({ length: n }, () => false) + ); + + for (let end = 0; end < n; end++) { + for (let start = end; start >= 0; start--) { + if (start === end) { + dp[start][end] = true; + continue; + } + + if (start + 1 === end) { + if (s[start] === s[end]) { + dp[start][end] = true; + } + continue; + } + + if (s[start] === s[end] && dp[start + 1][end - 1]) { + dp[start][end] = true; + continue; + } + } + } + + let answer = 0; + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (dp[i][j]) { + answer++; + } + } + } + + return answer; +}; From ca72dd45ac485eb182f47ceb1b725edbb49265d2 Mon Sep 17 00:00:00 2001 From: Paik Date: Wed, 1 Jan 2025 11:47:56 +0900 Subject: [PATCH 05/10] feat: 322. Coin Change --- coin-change/gwbaik9717.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 coin-change/gwbaik9717.js diff --git a/coin-change/gwbaik9717.js b/coin-change/gwbaik9717.js new file mode 100644 index 000000000..e2b0b78a2 --- /dev/null +++ b/coin-change/gwbaik9717.js @@ -0,0 +1,33 @@ +// n: amount m: length of coins +// Time complexity: O(n * m) + O(mlogm) +// Space complexity: O(n * m) + +/** + * @param {number[]} coins + * @param {number} amount + * @return {number} + */ +var coinChange = function (coins, amount) { + const dp = Array.from({ length: coins.length + 1 }, () => + Array.from({ length: amount + 1 }, () => Infinity) + ); + coins.sort((a, b) => a - b); + + for (let i = 0; i < coins.length + 1; i++) { + dp[i][0] = 0; + } + + for (let i = 1; i <= amount; i++) { + for (let j = 1; j <= coins.length; j++) { + const coin = coins[j - 1]; + + if (i >= coin) { + dp[j][i] = Math.min(dp[j][i], 1 + dp[j][i - coin]); + } + + dp[j][i] = Math.min(dp[j][i], dp[j - 1][i]); + } + } + + return dp.at(-1).at(-1) === Infinity ? -1 : dp.at(-1).at(-1); +}; From 0cdbba8b7bcaf5c35b57da4d7a8ab2df517c4508 Mon Sep 17 00:00:00 2001 From: Paik Date: Thu, 2 Jan 2025 07:58:14 +0900 Subject: [PATCH 06/10] refactor: feat: 21. Merge Two Sorted Lists --- merge-two-sorted-lists/gwbaik9717.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/merge-two-sorted-lists/gwbaik9717.js b/merge-two-sorted-lists/gwbaik9717.js index 513778a83..e7d1ab988 100644 --- a/merge-two-sorted-lists/gwbaik9717.js +++ b/merge-two-sorted-lists/gwbaik9717.js @@ -30,16 +30,12 @@ var mergeTwoLists = function (list1, list2) { current = current.next; } - while (list1) { - current.next = new ListNode(list1.val); - list1 = list1.next; - current = current.next; + if (list1) { + current.next = list1; } - while (list2) { - current.next = new ListNode(list2.val); - list2 = list2.next; - current = current.next; + if (list2) { + current.next = list2; } return answer.next; From 70f3ef181627a372d0079ec1b19983fd2edfee57 Mon Sep 17 00:00:00 2001 From: Paik Date: Thu, 2 Jan 2025 08:14:17 +0900 Subject: [PATCH 07/10] refactor: feat: 79. Word Search --- word-search/gwbaik9717.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/word-search/gwbaik9717.js b/word-search/gwbaik9717.js index 182641028..2b1db0944 100644 --- a/word-search/gwbaik9717.js +++ b/word-search/gwbaik9717.js @@ -1,6 +1,6 @@ // h: height of the board, w: width of the board, n: length of the word // Time complexity: O(h * w * 4**n) -// Space complexity: O(h * w + n) +// Space complexity: O(n) /** * @param {character[][]} board @@ -15,10 +15,6 @@ var exist = function (board, word) { const dy = [1, 0, -1, 0]; const dx = [0, 1, 0, -1]; - const checked = Array.from({ length: h }, () => - Array.from({ length: w }, () => 0) - ); - let answer = false; const dfs = (current, index) => { @@ -28,7 +24,8 @@ var exist = function (board, word) { } const [cy, cx] = current; - checked[cy][cx] = 1; + const value = board[cy][cx]; + board[cy][cx] = ""; for (let i = 0; i < dy.length; i++) { const ny = cy + dy[i]; @@ -40,14 +37,14 @@ var exist = function (board, word) { ny < h && nx >= 0 && nx < w && - checked[ny][nx] === 0 && + board[ny][nx] && word[ni] === board[ny][nx] ) { dfs([ny, nx], ni); } } - checked[cy][cx] = 0; + board[cy][cx] = value; }; for (let i = 0; i < h; i++) { From 6eb0ac0753ec9bca31221fac2b647f6ba2a664a7 Mon Sep 17 00:00:00 2001 From: Paik Date: Thu, 2 Jan 2025 08:45:14 +0900 Subject: [PATCH 08/10] refactor: feat: 322. Coin Change --- coin-change/gwbaik9717.js | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/coin-change/gwbaik9717.js b/coin-change/gwbaik9717.js index e2b0b78a2..752060ad9 100644 --- a/coin-change/gwbaik9717.js +++ b/coin-change/gwbaik9717.js @@ -1,6 +1,6 @@ // n: amount m: length of coins // Time complexity: O(n * m) + O(mlogm) -// Space complexity: O(n * m) +// Space complexity: O(n) /** * @param {number[]} coins @@ -8,26 +8,16 @@ * @return {number} */ var coinChange = function (coins, amount) { - const dp = Array.from({ length: coins.length + 1 }, () => - Array.from({ length: amount + 1 }, () => Infinity) - ); - coins.sort((a, b) => a - b); - - for (let i = 0; i < coins.length + 1; i++) { - dp[i][0] = 0; - } + const dp = Array.from({ length: amount + 1 }, () => Infinity); + dp[0] = 0; - for (let i = 1; i <= amount; i++) { - for (let j = 1; j <= coins.length; j++) { - const coin = coins[j - 1]; - - if (i >= coin) { - dp[j][i] = Math.min(dp[j][i], 1 + dp[j][i - coin]); - } + coins.sort((a, b) => a - b); - dp[j][i] = Math.min(dp[j][i], dp[j - 1][i]); + for (const coin of coins) { + for (let i = coin; i <= amount; i++) { + dp[i] = Math.min(dp[i], dp[i - coin] + 1); } } - return dp.at(-1).at(-1) === Infinity ? -1 : dp.at(-1).at(-1); + return dp.at(-1) === Infinity ? -1 : dp.at(-1); }; From 7812d91bbda845ce3541e5cca03e68d23012bad0 Mon Sep 17 00:00:00 2001 From: Paik Date: Fri, 3 Jan 2025 07:56:16 +0900 Subject: [PATCH 09/10] refactor: 21. Merge Two Sorted Lists --- merge-two-sorted-lists/gwbaik9717.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/merge-two-sorted-lists/gwbaik9717.js b/merge-two-sorted-lists/gwbaik9717.js index e7d1ab988..b83246666 100644 --- a/merge-two-sorted-lists/gwbaik9717.js +++ b/merge-two-sorted-lists/gwbaik9717.js @@ -20,10 +20,10 @@ var mergeTwoLists = function (list1, list2) { while (list1 && list2) { if (list1.val < list2.val) { - current.next = new ListNode(list1.val); + current.next = list1; list1 = list1.next; } else { - current.next = new ListNode(list2.val); + current.next = list2; list2 = list2.next; } From e7c0e56efc28f14d7271d0488a25bcd05038da43 Mon Sep 17 00:00:00 2001 From: Paik Date: Sat, 4 Jan 2025 09:28:45 +0900 Subject: [PATCH 10/10] refactor: 647. Palindromic Substrings --- palindromic-substrings/gwbaik9717.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/palindromic-substrings/gwbaik9717.js b/palindromic-substrings/gwbaik9717.js index 6d65d5e8b..69e042bd4 100644 --- a/palindromic-substrings/gwbaik9717.js +++ b/palindromic-substrings/gwbaik9717.js @@ -10,34 +10,28 @@ var countSubstrings = function (s) { const dp = Array.from({ length: n }, () => Array.from({ length: n }, () => false) ); + let answer = 0; for (let end = 0; end < n; end++) { for (let start = end; start >= 0; start--) { if (start === end) { dp[start][end] = true; + answer++; continue; } if (start + 1 === end) { if (s[start] === s[end]) { dp[start][end] = true; + answer++; } continue; } if (s[start] === s[end] && dp[start + 1][end - 1]) { dp[start][end] = true; - continue; - } - } - } - - let answer = 0; - - for (let i = 0; i < n; i++) { - for (let j = 0; j < n; j++) { - if (dp[i][j]) { answer++; + continue; } } }