From 0adea9c36e82356f124bc5ca6d51ca679d3000f4 Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Mon, 12 Aug 2024 22:29:43 +0900 Subject: [PATCH 1/5] Contains Duplicate Solution --- contains-duplicate/naringst.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 contains-duplicate/naringst.js diff --git a/contains-duplicate/naringst.js b/contains-duplicate/naringst.js new file mode 100644 index 000000000..4ceecd560 --- /dev/null +++ b/contains-duplicate/naringst.js @@ -0,0 +1,27 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ + +/** + * Runtime: 89ms, Memory: 63MB + * + * Time complexity: O(n) + * - To find the length of an array: O(n) + * - Array to Set: O(n) + * - To find the size of a set: O(n) + * Space complexity: O(n) + * - arrToSet: O(n) + * + * **/ + +var containsDuplicate = function (nums) { + const arrLength = nums.length; + const arrToSet = new Set(nums); + const setLength = arrToSet.size; + + if (arrLength !== setLength) { + return true; + } + return false; +}; From df1514a0193986dc3fb507d89227a27483a226fb Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Sat, 17 Aug 2024 15:17:50 +0900 Subject: [PATCH 2/5] Number of 1 bits Solution --- number-of-1-bits/naringst.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 number-of-1-bits/naringst.js diff --git a/number-of-1-bits/naringst.js b/number-of-1-bits/naringst.js new file mode 100644 index 000000000..02699b6c6 --- /dev/null +++ b/number-of-1-bits/naringst.js @@ -0,0 +1,29 @@ +/** + * @param {number} n + * @return {number} + */ + +/** + * Runtime: 59ms, Memory: 50.76MB + * + * Time complexity: O(logN) + * -> While + * Space complexity: O(logN) + * -> To save binary.split() + * + * **/ + +var hammingWeight = function (n) { + let binary = ""; + + while (n > 1) { + let left = n % 2; + binary += left.toString(); + n = Math.floor(n / 2); + } + binary = binary + n.toString(); + + const count = binary.split("1").length - 1; + + return count; +}; From d11c94dcd5d49eb67463e4fa2b0bc9ea69f6b26d Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Sat, 17 Aug 2024 15:20:01 +0900 Subject: [PATCH 3/5] Update contains-duplicate/naringst.js Co-authored-by: Evan Suhyeong Lee <37020415+sounmind@users.noreply.github.com> --- contains-duplicate/naringst.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/contains-duplicate/naringst.js b/contains-duplicate/naringst.js index 4ceecd560..5438d8ef0 100644 --- a/contains-duplicate/naringst.js +++ b/contains-duplicate/naringst.js @@ -20,8 +20,5 @@ var containsDuplicate = function (nums) { const arrToSet = new Set(nums); const setLength = arrToSet.size; - if (arrLength !== setLength) { - return true; - } - return false; + return arrLength !== setLength; }; From 7b150457ee5975f971e7e36d8cd0979714ecf32e Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Sat, 17 Aug 2024 16:11:31 +0900 Subject: [PATCH 4/5] Top K Frequent Elements Solution --- top-k-frequent-elements/naringst.js | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 top-k-frequent-elements/naringst.js diff --git a/top-k-frequent-elements/naringst.js b/top-k-frequent-elements/naringst.js new file mode 100644 index 000000000..fba48afef --- /dev/null +++ b/top-k-frequent-elements/naringst.js @@ -0,0 +1,30 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ + +/** + * Runtime: 64ms, Memory: 53.31MB + * + * Time complexity: O(NlogN) + * - frquentEntries.sort: NlogN + * Space complexity: O(n) + * + * **/ + +var topKFrequent = function (nums, k) { + let answer = []; + + const frequent = {}; + for (const num of nums) { + frequent[num] = frequent[num] ? frequent[num] + 1 : 1; + } + + const frequentEntries = Object.entries(frequent); + frequentEntries.sort((a, b) => b[1] - a[1]); + + const topK = frequentEntries.slice(0, k).map((i) => i[0]); + + return topK; +}; From 2fe91c146f93d9dfbc08a0407bffab28cb921df5 Mon Sep 17 00:00:00 2001 From: Nari Jeong <92130993+naringst@users.noreply.github.com> Date: Sat, 17 Aug 2024 23:46:26 +0900 Subject: [PATCH 5/5] Palindromic Substrings Solution --- palindromic-substrings/naringst.js | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 palindromic-substrings/naringst.js diff --git a/palindromic-substrings/naringst.js b/palindromic-substrings/naringst.js new file mode 100644 index 000000000..34305e375 --- /dev/null +++ b/palindromic-substrings/naringst.js @@ -0,0 +1,39 @@ +/** + * @param {string} s + * @return {number} + */ + +/** + * Runtime: 1521ms, Memory: 56.61MB + * + * Time complexity: O(N^2) + * Space complexity: O(N^2) + * + * Note: necessary to think of an alternative approach + * **/ + +function isPalindrome(subString) { + const len = subString.length; + for (let i = 0; i < len / 2; i++) { + if (subString[i] !== subString[len - 1 - i]) { + return false; + } + return true; + } +} + +var countSubstrings = function (s) { + const n = s.length; + let answer = n; + + for (let i = 0; i < n; i++) { + for (let j = i + 1; j < n; j++) { + let subString = s.slice(i, j + 1); + if (isPalindrome(subString)) { + answer += 1; + } + } + } + + return answer; +};