diff --git a/3sum/nhistory.js b/3sum/nhistory.js new file mode 100644 index 000000000..726f2a5ed --- /dev/null +++ b/3sum/nhistory.js @@ -0,0 +1,43 @@ +var threeSum = function (nums) { + // Sort nums array + const sortedNums = nums.sort((a, b) => a - b); + let result = []; + + // Start iteration to pick first element for 3sum + for (let i = 0; i < sortedNums.length; i++) { + // Check if the first element is already greater than 0, no valid triplets possible after this point + if (sortedNums[i] > 0) { + break; + } + + // Skip duplicates of the first element to avoid redundant triplets + if (i > 0 && sortedNums[i] === sortedNums[i - 1]) { + continue; + } + + // Iterate to find sum of two pointer and nums[i] + let left = i + 1; + let right = sortedNums.length - 1; + + while (left < right) { + let sum = sortedNums[i] + sortedNums[left] + sortedNums[right]; + + if (sum === 0) { + result.push([sortedNums[i], sortedNums[left], sortedNums[right]]); + // Skip duplicates of left and right pointers to avoid redundant triplets + while (sortedNums[left] === sortedNums[left + 1]) left++; + while (sortedNums[right] === sortedNums[right - 1]) right--; + left++; + right--; + } else if (sum < 0) { + left++; + } else if (sum > 0) { + right--; + } + } + } + return result; +}; + +// TC: O(n^2) +// SC: O(n) diff --git a/encode-and-decode-strings/nhistory.js b/encode-and-decode-strings/nhistory.js new file mode 100644 index 000000000..78b4dab4c --- /dev/null +++ b/encode-and-decode-strings/nhistory.js @@ -0,0 +1,43 @@ +class Solution { + /** + * @param {string[]} strs + * @returns {string} + */ + encode(strs) { + let result = ""; + for (let str of strs) { + result += str.length.toString() + "#" + str; + } + return result; + } + + /** + * @param {string} str + * @returns {string[]} + */ + decode(str) { + let result = []; + let i = 0; + + while (i < str.length) { + // Find the position of the next '#' + let j = i; + while (str[j] !== "#") { + j++; + } + + // Length of the next string + const len = parseInt(str.slice(i, j)); + i = j + 1; // Move past the '#' + + // Extract the string of length 'len' + result.push(str.slice(i, i + len)); + i += len; // Move past the extracted string + } + + return result; + } +} + +// TC: O(n),O(n) +// SC: O(n),O(n) diff --git a/longest-consecutive-sequence/nhistory.js b/longest-consecutive-sequence/nhistory.js new file mode 100644 index 000000000..763258459 --- /dev/null +++ b/longest-consecutive-sequence/nhistory.js @@ -0,0 +1,30 @@ +var longestConsecutive = function (nums) { + // Return 0 if there are no elements in nums + if (nums.length === 0) return 0; + + // Create a set to find values efficiently + const numSet = new Set(nums); + let maxLength = 0; + + for (let num of numSet) { + // Check if this is the start of a sequence + if (!numSet.has(num - 1)) { + let currentNum = num; + let currentLength = 1; + + // Count the length of the sequence + while (numSet.has(currentNum + 1)) { + currentNum += 1; + currentLength += 1; + } + + // Update the maximum length + maxLength = Math.max(maxLength, currentLength); + } + } + + return maxLength; +}; + +// TC: O(n) +// SC: O(n) diff --git a/product-of-array-except-self/nhistory.js b/product-of-array-except-self/nhistory.js new file mode 100644 index 000000000..ea5379d1f --- /dev/null +++ b/product-of-array-except-self/nhistory.js @@ -0,0 +1,40 @@ +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function (nums) { + const len = nums.length; + const lastIndex = len - 1; + + // Declare prefix, postfix array + let prefix = new Array(len).fill(1); + let postfix = new Array(len).fill(1); + + // Iterate loop to add prefix[i-1]*nums[i] values into prefix array + prefix[0] = nums[0]; + for (let i = 1; i < nums.length; i++) { + prefix[i] = prefix[i - 1] * nums[i]; + } + + // Iterate loop to add postfix[i]*nums[i-1] values into postfix array + postfix[lastIndex] = nums[lastIndex]; + for (let i = lastIndex; i >= 1; i--) { + postfix[i - 1] = postfix[i] * nums[i - 1]; + } + + // Make output array with prefix and postfix arrays + let output = new Array(len).fill(1); + + // First index value is equal to postfix[1] + output[0] = postfix[1]; + // Last index value is equal to prefix[lastIndex-1] + output[lastIndex] = prefix[lastIndex - 1]; + for (let i = 1; i < len - 1; i++) { + output[i] = prefix[i - 1] * postfix[i + 1]; + } + + return output; +}; + +// TC: O(n) +// SC: O(n) diff --git a/top-k-frequent-elements/nhistory.js b/top-k-frequent-elements/nhistory.js new file mode 100644 index 000000000..a99a2831a --- /dev/null +++ b/top-k-frequent-elements/nhistory.js @@ -0,0 +1,14 @@ +var topKFrequent = function (nums, k) { + // 1. Hashmap { num : frequency } + let map = {}; + + // 2. Iterate counts frequency of each value + for (num of nums) { + // Check there is already num key or not + if (!map[num]) map[num] = 0; + map[num]++; + } + + // 3. Sort frequency and return sliced array + return [...Object.keys(map)].sort((a, b) => map[b] - map[a]).slice(0, k); +};