From 2683ab227c61754f63b900055ab2f15cd4065f4e Mon Sep 17 00:00:00 2001 From: hyejjun Date: Mon, 12 Aug 2024 15:42:32 +0900 Subject: [PATCH 1/5] Constains Duplicate --- contains-duplicate/hyejjun.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 contains-duplicate/hyejjun.js diff --git a/contains-duplicate/hyejjun.js b/contains-duplicate/hyejjun.js new file mode 100644 index 000000000..a72125e9b --- /dev/null +++ b/contains-duplicate/hyejjun.js @@ -0,0 +1,16 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function (nums) { + + const set = new Set(nums); + + return nums.length !== set.size ? true : false; + +}; + + +console.log(containsDuplicate([1, 2, 3, 1])); // true +console.log(containsDuplicate([1, 2, 3, 4])); // false +console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true From 0ada062f1cc2888ff55a07ea99b9ead59bfef3f2 Mon Sep 17 00:00:00 2001 From: hyejjun Date: Tue, 13 Aug 2024 15:49:38 +0900 Subject: [PATCH 2/5] Number Of 1 Bits --- number-of-1-bits/hyejjun.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 number-of-1-bits/hyejjun.js diff --git a/number-of-1-bits/hyejjun.js b/number-of-1-bits/hyejjun.js new file mode 100644 index 000000000..596071e1c --- /dev/null +++ b/number-of-1-bits/hyejjun.js @@ -0,0 +1,20 @@ +/** + * @param {number} n + * @return {number} + */ +var hammingWeight = function (n) { + + let val = n.toString(2); + + let res = 0; + [...val].forEach((val) => res += parseInt(val)) + + return res; + +}; + +// O(LogN) + +console.log(hammingWeight(11)); +console.log(hammingWeight(128)); +console.log(hammingWeight(2147483645)); From b776697a07dca92e564c3488649e6d3534a345d9 Mon Sep 17 00:00:00 2001 From: hyejjun Date: Fri, 16 Aug 2024 16:59:05 +0900 Subject: [PATCH 3/5] Top K Frequent Elements --- top-k-frequent-elements/hyejjun.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 top-k-frequent-elements/hyejjun.js diff --git a/top-k-frequent-elements/hyejjun.js b/top-k-frequent-elements/hyejjun.js new file mode 100644 index 000000000..1b1051f42 --- /dev/null +++ b/top-k-frequent-elements/hyejjun.js @@ -0,0 +1,25 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function (nums, k) { + const count = {}; + + nums.forEach((num) => { + count[num] = (count[num] || 0) + 1; + }); + + const filtered = Object.keys(count).sort((a, b) => count[b] - count[a]); + + return filtered.slice(0, k).map(Number); + +}; + +console.log(topKFrequent([1, 1, 1, 2, 2, 3], 2)); // [1, 2] +console.log(topKFrequent([1], 1)); // [1] + +/* +Time Complexity : O(NLogN) +Space Complexity: O(N) +*/ \ No newline at end of file From 1d1cecae98e8fecd6c4d36a45c6d8282aeb91e51 Mon Sep 17 00:00:00 2001 From: hyejjun Date: Fri, 16 Aug 2024 18:24:42 +0900 Subject: [PATCH 4/5] Palindromic Substrings --- palindromic-substrings/hyejjun.js | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 palindromic-substrings/hyejjun.js diff --git a/palindromic-substrings/hyejjun.js b/palindromic-substrings/hyejjun.js new file mode 100644 index 000000000..49cf17b7c --- /dev/null +++ b/palindromic-substrings/hyejjun.js @@ -0,0 +1,32 @@ +/** + * @param {string} s + * @return {number} + */ +var countSubstrings = function (s) { + let count = 0; + + function checkPalindromic(left, right) { + while (left >= 0 && right < s.length && s[left] === s[right]) { + count++; + left--; + right++; + } + + } + + for (let i = 0; i < s.length; i++) { + checkPalindromic(i, i); + checkPalindromic(i, i + 1); + } + + return count; +}; + +console.log(countSubstrings("abc")); +console.log(countSubstrings("aaa")); + + +/* +Time Complexity : O(n^2) +Space Complexity: O(1) +*/ \ No newline at end of file From 0120b1c0dbd1f896b554d884ca83cb5b45dd0c05 Mon Sep 17 00:00:00 2001 From: hyejjun Date: Sat, 17 Aug 2024 21:52:38 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=EB=A7=88=EC=A7=80=EB=A7=89=EC=A4=84=20?= =?UTF-8?q?=EA=B0=9C=ED=96=89=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- palindromic-substrings/hyejjun.js | 2 +- top-k-frequent-elements/hyejjun.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/palindromic-substrings/hyejjun.js b/palindromic-substrings/hyejjun.js index 49cf17b7c..23e824ee4 100644 --- a/palindromic-substrings/hyejjun.js +++ b/palindromic-substrings/hyejjun.js @@ -29,4 +29,4 @@ console.log(countSubstrings("aaa")); /* Time Complexity : O(n^2) Space Complexity: O(1) -*/ \ No newline at end of file +*/ diff --git a/top-k-frequent-elements/hyejjun.js b/top-k-frequent-elements/hyejjun.js index 1b1051f42..ce7c3aae6 100644 --- a/top-k-frequent-elements/hyejjun.js +++ b/top-k-frequent-elements/hyejjun.js @@ -22,4 +22,4 @@ console.log(topKFrequent([1], 1)); // [1] /* Time Complexity : O(NLogN) Space Complexity: O(N) -*/ \ No newline at end of file +*/