diff --git a/contains-duplicate/naringst.js b/contains-duplicate/naringst.js new file mode 100644 index 000000000..5438d8ef0 --- /dev/null +++ b/contains-duplicate/naringst.js @@ -0,0 +1,24 @@ +/** + * @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; + + return arrLength !== setLength; +}; 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; +}; 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; +}; 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; +};