From 2f8a34bd3de42755e461a72fb35a940b837b6f6b Mon Sep 17 00:00:00 2001 From: Paik Date: Wed, 18 Dec 2024 08:54:07 +0900 Subject: [PATCH 1/6] feat: 242. Valid Anagram --- valid-anagram/gwbaik9717.js | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 valid-anagram/gwbaik9717.js diff --git a/valid-anagram/gwbaik9717.js b/valid-anagram/gwbaik9717.js new file mode 100644 index 000000000..d764880ec --- /dev/null +++ b/valid-anagram/gwbaik9717.js @@ -0,0 +1,39 @@ +// Time complexity: O(n) +// Space complexity: O(n) + +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function (s, t) { + if (s.length !== t.length) { + return false; + } + + const createDictFromString = (str) => { + const dict = new Map(); + + for (const chr of str) { + if (dict.has(chr)) { + dict.set(chr, dict.get(chr) + 1); + continue; + } + + dict.set(chr, 1); + } + + return dict; + }; + + const dictS = createDictFromString(s); + const dictT = createDictFromString(t); + + for (const [key, value] of dictS) { + if (dictT.get(key) !== value) { + return false; + } + } + + return true; +}; From 82a8df9a25538c62f34836c9ee7f81d3e35abacb Mon Sep 17 00:00:00 2001 From: Paik Date: Wed, 18 Dec 2024 09:07:44 +0900 Subject: [PATCH 2/6] feat: 70. Climbing Stairs --- climbing-stairs/gwbaik9717.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 climbing-stairs/gwbaik9717.js diff --git a/climbing-stairs/gwbaik9717.js b/climbing-stairs/gwbaik9717.js new file mode 100644 index 000000000..6f5a8eaa7 --- /dev/null +++ b/climbing-stairs/gwbaik9717.js @@ -0,0 +1,18 @@ +// Time complexity: O(n) +// Space complexity: O(n) + +/** + * @param {number} n + * @return {number} + */ +var climbStairs = function (n) { + const dp = Array.from({ length: n + 1 }, () => 0); + dp[0] = 1; + dp[1] = 1; + + for (let i = 2; i <= n; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + + return dp[n]; +}; From 1eb02cbf21a8b301243e040d9ec5ad0a419bf46c Mon Sep 17 00:00:00 2001 From: Paik Date: Wed, 18 Dec 2024 09:20:35 +0900 Subject: [PATCH 3/6] refactor: 70. Climbing Stairs to reduce Space complexity to O(1) --- climbing-stairs/gwbaik9717.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/climbing-stairs/gwbaik9717.js b/climbing-stairs/gwbaik9717.js index 6f5a8eaa7..e50012d57 100644 --- a/climbing-stairs/gwbaik9717.js +++ b/climbing-stairs/gwbaik9717.js @@ -1,18 +1,16 @@ // Time complexity: O(n) -// Space complexity: O(n) +// Space complexity: O(1) /** * @param {number} n * @return {number} */ var climbStairs = function (n) { - const dp = Array.from({ length: n + 1 }, () => 0); - dp[0] = 1; - dp[1] = 1; + const dp = [1, 1]; for (let i = 2; i <= n; i++) { - dp[i] = dp[i - 1] + dp[i - 2]; + dp[i % 2] = dp[0] + dp[1]; } - return dp[n]; + return dp[n % 2]; }; From ddc84aa2adc4b678b7799dfe776b71172dc93e53 Mon Sep 17 00:00:00 2001 From: Paik Date: Thu, 19 Dec 2024 08:45:43 +0900 Subject: [PATCH 4/6] feat: 15. 3Sum --- 3sum/gwbaik9717.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 3sum/gwbaik9717.js diff --git a/3sum/gwbaik9717.js b/3sum/gwbaik9717.js new file mode 100644 index 000000000..5f38ac96f --- /dev/null +++ b/3sum/gwbaik9717.js @@ -0,0 +1,38 @@ +// Time complexity: O(n^2) +// Space complexity: O(n) + +/** + * @param {number[]} nums + * @return {number[][]} + */ +var threeSum = function (nums) { + const sumsDict = new Set(); + const sortedNums = nums.toSorted((a, b) => a - b); + + const n = nums.length; + + for (let i = 0; i < n - 2; i++) { + let left = i + 1; + let right = n - 1; + const fixed = sortedNums[i]; + + const targetSum = 0 - fixed; + + while (left < right) { + const currentSum = sortedNums[left] + sortedNums[right]; + + if (currentSum < targetSum) { + left++; + } else if (currentSum > targetSum) { + right--; + } else { + const key = [fixed, sortedNums[left], sortedNums[right]]; + sumsDict.add(key.join(",")); + left++; + right--; + } + } + } + + return Array.from(sumsDict).map((nums) => nums.split(",").map(Number)); +}; From 35e5aaa5574496c7811c348478e544fa8f2b3c7c Mon Sep 17 00:00:00 2001 From: Paik Date: Sat, 21 Dec 2024 10:48:07 +0900 Subject: [PATCH 5/6] feat: 105. Construct Binary Tree from Preorder and Inorder Traversal --- .../gwbaik9717.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/gwbaik9717.js diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/gwbaik9717.js b/construct-binary-tree-from-preorder-and-inorder-traversal/gwbaik9717.js new file mode 100644 index 000000000..32cdd9da7 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/gwbaik9717.js @@ -0,0 +1,27 @@ +// Time complexity: O(n^2) +// Space complexity: O(n) + +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {number[]} preorder + * @param {number[]} inorder + * @return {TreeNode} + */ +var buildTree = function (preorder, inorder) { + if (!preorder.length || !inorder.length) return null; + + const root = new TreeNode(preorder[0]); + const mid = inorder.indexOf(root.val); + + root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid)); + root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1)); + + return root; +}; From 3168fa4a5601cc7d0f5137b16b0b4dd1720b9994 Mon Sep 17 00:00:00 2001 From: Paik Date: Sat, 21 Dec 2024 20:36:18 +0900 Subject: [PATCH 6/6] feat: 91. Decode Ways --- decode-ways/gwbaik9717.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 decode-ways/gwbaik9717.js diff --git a/decode-ways/gwbaik9717.js b/decode-ways/gwbaik9717.js new file mode 100644 index 000000000..ad33f80c3 --- /dev/null +++ b/decode-ways/gwbaik9717.js @@ -0,0 +1,38 @@ +// Time complexity: O(n) +// Space complexity: O(n) + +/** + * @param {string} s + * @return {number} + */ +var numDecodings = function (s) { + const n = s.length; + const dp = Array.from({ length: n + 2 }, () => 0); + dp[1] = 1; + + for (let i = 2; i < n + 2; i++) { + // 한자리 + const charCode = Number(s[i - 2]); + + if (charCode > 0) { + dp[i] += dp[i - 1]; + } + + // 두자리 + if (i <= 2) { + continue; + } + + if (Number(s[i - 3]) == 0) { + continue; + } + + const strCode = Number(s.slice(i - 3, i - 1)); + + if (strCode > 0 && strCode <= 26) { + dp[i] += dp[i - 2]; + } + } + + return dp.at(-1); +};