From ae583465167bfae43aa0d535e7ac30380eb657a9 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Fri, 23 Aug 2024 23:56:53 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=EB=AC=B8=EC=A0=9C=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- counting-bits/hwanmini.js | 21 +++++++++++++++++++++ valid-anagram/hwanmini.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 counting-bits/hwanmini.js create mode 100644 valid-anagram/hwanmini.js diff --git a/counting-bits/hwanmini.js b/counting-bits/hwanmini.js new file mode 100644 index 000000000..405c1db50 --- /dev/null +++ b/counting-bits/hwanmini.js @@ -0,0 +1,21 @@ +// 시간복잡도 O(n * log n) +// 공간복잡도 O(n) + +/** + * @param {number} n + * @return {number[]} + */ +var countBits = function(n) { + const result = [] + + for (let i = 0 ; i <= n; i++) { + const binaryNumber = i.toString(2) + const oneLength = binaryNumber.replaceAll('0','').length + + result.push(oneLength) + } + + return result +}; + +console.log(countBits(5)) \ No newline at end of file diff --git a/valid-anagram/hwanmini.js b/valid-anagram/hwanmini.js new file mode 100644 index 000000000..81222fd35 --- /dev/null +++ b/valid-anagram/hwanmini.js @@ -0,0 +1,28 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(k) + +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s, t) { + if (s.length !== t.length) return false + + const checkMap = new Map(); + + for (let i = 0; i < s.length; i++) { + checkMap.set(s[i], (checkMap.get(s[i]) || 0) + 1) + } + + + for (let j = 0; j < t.length; j++) { + if (!checkMap.get(t[j]) || checkMap.get(t[j]) < 0) return false + checkMap.set(t[j], (checkMap.get(t[j]) || 0 ) - 1); + } + + return true +}; + + +console.log(isAnagram("anagram","nagaram")) \ No newline at end of file From 72f122b82fbf5bdaf2c090456bc45c4731dfd788 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Fri, 23 Aug 2024 23:59:16 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20line=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- counting-bits/hwanmini.js | 2 +- valid-anagram/hwanmini.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/counting-bits/hwanmini.js b/counting-bits/hwanmini.js index 405c1db50..92c957a99 100644 --- a/counting-bits/hwanmini.js +++ b/counting-bits/hwanmini.js @@ -18,4 +18,4 @@ var countBits = function(n) { return result }; -console.log(countBits(5)) \ No newline at end of file +console.log(countBits(5)) diff --git a/valid-anagram/hwanmini.js b/valid-anagram/hwanmini.js index 81222fd35..f7e2987dc 100644 --- a/valid-anagram/hwanmini.js +++ b/valid-anagram/hwanmini.js @@ -25,4 +25,4 @@ var isAnagram = function(s, t) { }; -console.log(isAnagram("anagram","nagaram")) \ No newline at end of file +console.log(isAnagram("anagram","nagaram")) From 41903740d102664451bcef2a98594d8ede6021ab Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sun, 25 Aug 2024 02:38:22 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20buildTree=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=ED=92=80=EC=9D=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hwanmini.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/hwanmini.js diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/hwanmini.js b/construct-binary-tree-from-preorder-and-inorder-traversal/hwanmini.js new file mode 100644 index 000000000..d1dd2c7f4 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/hwanmini.js @@ -0,0 +1,14 @@ +// 시간복잡도: O(n2) +// 공간복잡도: O(n) + +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; +}; \ No newline at end of file From d2aa85f047a5d60f5aebd060227be393a3b58b3c Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sun, 25 Aug 2024 02:38:46 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20line=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hwanmini.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/hwanmini.js b/construct-binary-tree-from-preorder-and-inorder-traversal/hwanmini.js index d1dd2c7f4..de2a17a47 100644 --- a/construct-binary-tree-from-preorder-and-inorder-traversal/hwanmini.js +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/hwanmini.js @@ -11,4 +11,4 @@ var buildTree = function(preorder, inorder) { root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1)); return root; -}; \ No newline at end of file +};