From 7c5c3cfc61dcdc8423c35207b0234b2f2915452d Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sun, 18 Aug 2024 12:04:33 +0900 Subject: [PATCH 1/8] solve: valid anagram --- valid-anagram/wogha95.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 valid-anagram/wogha95.js diff --git a/valid-anagram/wogha95.js b/valid-anagram/wogha95.js new file mode 100644 index 000000000..90b723358 --- /dev/null +++ b/valid-anagram/wogha95.js @@ -0,0 +1,11 @@ +// TC: O(N * log N) +// SC: O(N) + +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s, t) { + return s.split('').sort().join('') === t.split('').sort().join(''); +}; \ No newline at end of file From 6d43c3979199892201bebf7534201865306f3c6b Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sun, 18 Aug 2024 12:10:52 +0900 Subject: [PATCH 2/8] fix: line lint --- valid-anagram/wogha95.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/valid-anagram/wogha95.js b/valid-anagram/wogha95.js index 90b723358..22b2f74b9 100644 --- a/valid-anagram/wogha95.js +++ b/valid-anagram/wogha95.js @@ -8,4 +8,4 @@ */ var isAnagram = function(s, t) { return s.split('').sort().join('') === t.split('').sort().join(''); -}; \ No newline at end of file +}; From 3855d02c906b6ff7dcd8b2fd83466357c7aa2407 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Mon, 19 Aug 2024 22:28:23 +0900 Subject: [PATCH 3/8] solve: counting bits --- counting-bits/wogha95.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 counting-bits/wogha95.js diff --git a/counting-bits/wogha95.js b/counting-bits/wogha95.js new file mode 100644 index 000000000..1f2ac9e4b --- /dev/null +++ b/counting-bits/wogha95.js @@ -0,0 +1,25 @@ +// TC: O(N) +// SC: O(N) + +/** + * @param {number} n + * @return {number[]} + */ +var countBits = function (n) { + const result = [0]; + let pointer = 0; + let lastPointer = 0; + + for (let num = 1; num <= n; num++) { + result.push(result[pointer] + 1); + + if (pointer === lastPointer) { + lastPointer = result.length - 1; + pointer = 0; + } else { + pointer += 1; + } + } + + return result; +}; From a0ef72e2a0d7e4a116f471977fa9adf7a5bab1ba Mon Sep 17 00:00:00 2001 From: wogha95 Date: Wed, 21 Aug 2024 21:55:43 +0900 Subject: [PATCH 4/8] solve: construct binary tree from preorder and inorder traversal --- .../wogha95.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js b/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js new file mode 100644 index 000000000..8c1cf91ac --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js @@ -0,0 +1,37 @@ +// TC: O(N) - leetcode analyze 기준 +// SC: 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 (inorder.length === 0) { + return null; + } + + if (inorder.length === 1) { + return new TreeNode(inorder[0]); + } + + const rootValue = preorder[0]; + const leftNodeLength = inorder.findIndex((value) => value === rootValue); + const leftNode = buildTree( + preorder.slice(1, 1 + leftNodeLength), + inorder.slice(0, leftNodeLength) + ); + const rightNode = buildTree( + preorder.slice(1 + leftNodeLength), + inorder.slice(leftNodeLength + 1) + ); + return new TreeNode(rootValue, leftNode, rightNode); +}; From 4f38c55709af3d0e2ded8673901dba5ca091c750 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sat, 24 Aug 2024 20:30:33 +0900 Subject: [PATCH 5/8] solve: decode ways --- decode-ways/wogha95.js | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 decode-ways/wogha95.js diff --git a/decode-ways/wogha95.js b/decode-ways/wogha95.js new file mode 100644 index 000000000..450467e96 --- /dev/null +++ b/decode-ways/wogha95.js @@ -0,0 +1,48 @@ +// TC: O(N) +// SC: O(N) + +/** + * @param {string} s + * @return {number} + */ +var numDecodings = function (s) { + if (s[0] === "0") { + return 0; + } + if (s.length === 1) { + return 1; + } + + const dpTable = new Array(s.length).fill(0); + if (s[0] !== "0") { + dpTable[0] = 1; + } + if (s[1] !== "0") { + dpTable[1] += 1; + } + if (isValid(`${s[0]}${s[1]}`)) { + dpTable[1] += 1; + } + + for (let index = 2; index < s.length; index++) { + if (s[index] !== "0") { + dpTable[index] += dpTable[index - 1]; + } + if (s[index - 1] !== "0" && isValid(`${s[index - 1]}${s[index]}`)) { + dpTable[index] += dpTable[index - 2]; + } + } + + return dpTable[dpTable.length - 1]; + + function isValid(stringNumber) { + const number = Number(stringNumber); + if (number <= 0) { + return false; + } + if (27 <= number) { + return false; + } + return true; + } +}; From ff09bff3c3b7e237c3bec95ded091e15efa01685 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sat, 24 Aug 2024 20:50:33 +0900 Subject: [PATCH 6/8] review: construct binary tree from preorder and inorder traversal --- .../wogha95.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js b/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js index 8c1cf91ac..a1b2961fe 100644 --- a/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js @@ -19,10 +19,6 @@ var buildTree = function (preorder, inorder) { return null; } - if (inorder.length === 1) { - return new TreeNode(inorder[0]); - } - const rootValue = preorder[0]; const leftNodeLength = inorder.findIndex((value) => value === rootValue); const leftNode = buildTree( From 835af9b3defe95422fdd1cb270b8adf1cd1ece95 Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sun, 25 Aug 2024 15:53:39 +0900 Subject: [PATCH 7/8] review: construct binary tree from preorder and inorder traversal --- .../wogha95.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js b/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js index a1b2961fe..be1ef5f9e 100644 --- a/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js @@ -1,4 +1,4 @@ -// TC: O(N) - leetcode analyze 기준 +// TC: O(N^2) // SC: O(N) /** From 6388615ac906d10361c34fb81500133599929a7c Mon Sep 17 00:00:00 2001 From: wogha95 Date: Sun, 25 Aug 2024 15:56:44 +0900 Subject: [PATCH 8/8] review: construct binary tree from preorder and inorder traversal --- .../wogha95.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js b/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js index be1ef5f9e..dcdc748ab 100644 --- a/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/wogha95.js @@ -1,5 +1,5 @@ // TC: O(N^2) -// SC: O(N) +// SC: O(N^2) /** * Definition for a binary tree node.