From 9e6d539d7bdf75594e89ccc433a23d96b540421a Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Thu, 15 Aug 2024 20:06:09 +0900 Subject: [PATCH 01/10] =?UTF-8?q?feat:=20hammingWeight=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=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 --- number-of-1-bits/hwanmini.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 number-of-1-bits/hwanmini.js diff --git a/number-of-1-bits/hwanmini.js b/number-of-1-bits/hwanmini.js new file mode 100644 index 000000000..dca0de297 --- /dev/null +++ b/number-of-1-bits/hwanmini.js @@ -0,0 +1,20 @@ +// 시간복잡도: O(log n) +// 공간복잡도: O(log n) + +const replaceZeroToEmptyString = (str) => str.replaceAll('0','') + + +/** + * @param {number} n + * @return {number} + */ +var hammingWeight = function(n) { + const binaryNum = n.toString(2) + const replacedNumber = replaceZeroToEmptyString(binaryNum) + return replacedNumber.length +}; + + +console.log(hammingWeight(11)); +console.log(hammingWeight(128)); +console.log(hammingWeight(2147483645)); From 7011808a857c2da9c4235f9ea863424e0b81b976 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Thu, 15 Aug 2024 20:06:16 +0900 Subject: [PATCH 02/10] =?UTF-8?q?feat:=20topKFrequent=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 --- top-k-frequent-elements/hwanmini.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 top-k-frequent-elements/hwanmini.js diff --git a/top-k-frequent-elements/hwanmini.js b/top-k-frequent-elements/hwanmini.js new file mode 100644 index 000000000..47dce9054 --- /dev/null +++ b/top-k-frequent-elements/hwanmini.js @@ -0,0 +1,23 @@ +// 시간복잡도: O(n log n) +// 공간복잡도: O(n) + +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function(nums, k) { + const map = new Map() + + for (let i = 0; i < nums.length; i++) { + map.set(nums[i], (map.get(nums[i]) || 0) + 1); + } + + const frequencyArr = [...map] + const sortedArr = frequencyArr.toSorted((a,b) => b[1] - a[1]) + + return sortedArr.slice(0,k).map(([key,value]) => key); + +}; + +console.log(topKFrequent([1,1,1,2,2,3],2)) \ No newline at end of file From 5d155da678f53dc3cbd32c79705539ddff64d7cf Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Thu, 15 Aug 2024 20:06:22 +0900 Subject: [PATCH 03/10] =?UTF-8?q?feat:=20containsDuplicate=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=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 --- contains-duplicate/hwanminini.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 contains-duplicate/hwanminini.js diff --git a/contains-duplicate/hwanminini.js b/contains-duplicate/hwanminini.js new file mode 100644 index 000000000..4ef3b787f --- /dev/null +++ b/contains-duplicate/hwanminini.js @@ -0,0 +1,10 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function(nums) { + return nums.length !== new Set(nums).size +}; \ No newline at end of file From 677c2c0e3a1782e004953702bbaf52e3b8fa7015 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Thu, 15 Aug 2024 20:18:10 +0900 Subject: [PATCH 04/10] =?UTF-8?q?feat:=20last-line=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/hwanminini.js | 2 +- top-k-frequent-elements/hwanmini.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/contains-duplicate/hwanminini.js b/contains-duplicate/hwanminini.js index 4ef3b787f..efe53211a 100644 --- a/contains-duplicate/hwanminini.js +++ b/contains-duplicate/hwanminini.js @@ -7,4 +7,4 @@ */ var containsDuplicate = function(nums) { return nums.length !== new Set(nums).size -}; \ No newline at end of file +}; diff --git a/top-k-frequent-elements/hwanmini.js b/top-k-frequent-elements/hwanmini.js index 47dce9054..e5838ca1c 100644 --- a/top-k-frequent-elements/hwanmini.js +++ b/top-k-frequent-elements/hwanmini.js @@ -20,4 +20,4 @@ var topKFrequent = function(nums, k) { }; -console.log(topKFrequent([1,1,1,2,2,3],2)) \ No newline at end of file +console.log(topKFrequent([1,1,1,2,2,3],2)) From d616ef2aa24e6d5112ab79472df52ee36113670f Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sat, 17 Aug 2024 13:04:51 +0900 Subject: [PATCH 05/10] =?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 --- kth-smallest-element-in-a-bst/hwanminini.js | 29 +++++++++++++++++ palindromic-substrings/hwanminini.js | 35 +++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/hwanminini.js create mode 100644 palindromic-substrings/hwanminini.js diff --git a/kth-smallest-element-in-a-bst/hwanminini.js b/kth-smallest-element-in-a-bst/hwanminini.js new file mode 100644 index 000000000..54d66c542 --- /dev/null +++ b/kth-smallest-element-in-a-bst/hwanminini.js @@ -0,0 +1,29 @@ +// 시간 복잡도: O(n) +// 공간 복잡도: 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 {TreeNode} root + * @param {number} k + * @return {number} + */ +var kthSmallest = function(root, k) { + const results = [] + + const dfs = (tree) => { + results.push(tree.val) + if (tree.left) dfs(tree.left) + if (tree.right) dfs(tree.right) + } + + dfs(root) + + return results[k-1] +}; \ No newline at end of file diff --git a/palindromic-substrings/hwanminini.js b/palindromic-substrings/hwanminini.js new file mode 100644 index 000000000..53c4eb979 --- /dev/null +++ b/palindromic-substrings/hwanminini.js @@ -0,0 +1,35 @@ +// 시간 복잡도: O(n^3) +// 공간 복잡도: O(1) + +/** + * @param {string} s + * @return {number} + */ +var countSubstrings = function(s) { + let count = 0; + + let plusIndex = 0; + while (plusIndex !== s.length) { + for (let i = 0 ; i < s.length - plusIndex; i++) { + if (isValidSubstring(s, i, i + plusIndex)) count++ + } + + plusIndex++; + } + + return count; +}; + + +function isValidSubstring(s, left, right) { + while (left <= right) { + if (s[left] !== s[right]) return false; + + left++; + right--; + } + return true; +} + +console.log(countSubstrings("abc")); +console.log(countSubstrings("aaa")); From 1d001020f9512919a0775a0ca47d1129fff44d95 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sat, 17 Aug 2024 17:55:46 +0900 Subject: [PATCH 06/10] =?UTF-8?q?feat:=20o(n)=EB=AC=B8=EC=A0=9C=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20=EB=B0=A9=EC=8B=9D=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- top-k-frequent-elements/hwanmini.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/top-k-frequent-elements/hwanmini.js b/top-k-frequent-elements/hwanmini.js index e5838ca1c..4823e1596 100644 --- a/top-k-frequent-elements/hwanmini.js +++ b/top-k-frequent-elements/hwanmini.js @@ -1,4 +1,4 @@ -// 시간복잡도: O(n log n) +// 시간복잡도: O(n) // 공간복잡도: O(n) /** @@ -13,10 +13,18 @@ var topKFrequent = function(nums, k) { map.set(nums[i], (map.get(nums[i]) || 0) + 1); } - const frequencyArr = [...map] - const sortedArr = frequencyArr.toSorted((a,b) => b[1] - a[1]) + const buckets = Array.from({length: nums.length + 1}, () => []) - return sortedArr.slice(0,k).map(([key,value]) => key); + for (const [num, frequency] of map.entries()) { + buckets[frequency].push(num); + } + + const result = []; + for (let i = buckets.length - 1; i >= 0 && result.length < k; i--) { + result.push(...buckets[i]); + } + + return result.slice(0, k); }; From bfebdd2d9bddf4ba07ab240fc182e79840c66d57 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sat, 17 Aug 2024 17:57:21 +0900 Subject: [PATCH 07/10] =?UTF-8?q?feat:=20slice=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- top-k-frequent-elements/hwanmini.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/top-k-frequent-elements/hwanmini.js b/top-k-frequent-elements/hwanmini.js index 4823e1596..2509f3c6d 100644 --- a/top-k-frequent-elements/hwanmini.js +++ b/top-k-frequent-elements/hwanmini.js @@ -24,8 +24,7 @@ var topKFrequent = function(nums, k) { result.push(...buckets[i]); } - return result.slice(0, k); - + return result }; console.log(topKFrequent([1,1,1,2,2,3],2)) From 400b4d6532382f0b9a32a81a4150e49ab0e76054 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sun, 18 Aug 2024 10:09:49 +0900 Subject: [PATCH 08/10] =?UTF-8?q?feat:=20line=20break=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kth-smallest-element-in-a-bst/hwanminini.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kth-smallest-element-in-a-bst/hwanminini.js b/kth-smallest-element-in-a-bst/hwanminini.js index 54d66c542..bf2021949 100644 --- a/kth-smallest-element-in-a-bst/hwanminini.js +++ b/kth-smallest-element-in-a-bst/hwanminini.js @@ -26,4 +26,4 @@ var kthSmallest = function(root, k) { dfs(root) return results[k-1] -}; \ No newline at end of file +}; From 81e202dab9acadafde294cce962a02fc905029ad Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sun, 18 Aug 2024 11:16:30 +0900 Subject: [PATCH 09/10] =?UTF-8?q?feat:=20sort=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kth-smallest-element-in-a-bst/hwanminini.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kth-smallest-element-in-a-bst/hwanminini.js b/kth-smallest-element-in-a-bst/hwanminini.js index bf2021949..f34183f81 100644 --- a/kth-smallest-element-in-a-bst/hwanminini.js +++ b/kth-smallest-element-in-a-bst/hwanminini.js @@ -25,5 +25,7 @@ var kthSmallest = function(root, k) { dfs(root) + results.sort((a,b) => a - b) + return results[k-1] }; From 074bd58815e6e0f0de25e8af1ed84b2b6ce24d95 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sun, 18 Aug 2024 11:29:50 +0900 Subject: [PATCH 10/10] =?UTF-8?q?feat:=20=EC=8B=9C=EA=B0=84=20=EB=B3=B5?= =?UTF-8?q?=EC=9E=A1=EB=8F=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kth-smallest-element-in-a-bst/hwanminini.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kth-smallest-element-in-a-bst/hwanminini.js b/kth-smallest-element-in-a-bst/hwanminini.js index f34183f81..1c9b30500 100644 --- a/kth-smallest-element-in-a-bst/hwanminini.js +++ b/kth-smallest-element-in-a-bst/hwanminini.js @@ -1,4 +1,4 @@ -// 시간 복잡도: O(n) +// 시간 복잡도: O(n log n) // 공간 복잡도: O(n) /**