From 44eaaa602cb81513d1b3d41e783564796259e2a4 Mon Sep 17 00:00:00 2001 From: sounmind Date: Mon, 27 May 2024 20:54:31 -0400 Subject: [PATCH 1/6] solve: 3sum --- 3sum/evan.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 3sum/evan.js diff --git a/3sum/evan.js b/3sum/evan.js new file mode 100644 index 000000000..cf97534bc --- /dev/null +++ b/3sum/evan.js @@ -0,0 +1,49 @@ +/** + * @param {number[]} nums + * @return {number[][]} + */ +var threeSum = function (nums) { + const sorted = nums.sort((a, b) => a - b); + const result = []; + + for (let i = 0; i < sorted.length; i++) { + const fixedNumber = sorted[i]; + const previousFixedNumber = sorted[i - 1]; + + if (fixedNumber === previousFixedNumber) { + continue; + } + + let [leftEnd, rightEnd] = [i + 1, sorted.length - 1]; + + while (leftEnd < rightEnd) { + const sum = fixedNumber + sorted[leftEnd] + sorted[rightEnd]; + + if (sum === 0) { + result.push([sorted[leftEnd], sorted[rightEnd], sorted[i]]); + + while ( + sorted[leftEnd + 1] === sorted[leftEnd] || + sorted[rightEnd - 1] === sorted[rightEnd] + ) { + if (sorted[leftEnd + 1] === sorted[leftEnd]) { + leftEnd += 1; + } + + if (sorted[rightEnd - 1] === sorted[rightEnd]) { + rightEnd -= 1; + } + } + + leftEnd += 1; + rightEnd -= 1; + } else if (sum < 0) { + leftEnd += 1; + } else { + rightEnd -= 1; + } + } + } + + return result; +}; From 35b3b15fe8152fbf15ff1ba2a2e24c9bff355133 Mon Sep 17 00:00:00 2001 From: sounmind Date: Tue, 28 May 2024 18:20:58 -0400 Subject: [PATCH 2/6] solve: top K frequent elements --- top-k-frequent-elements/evan.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 top-k-frequent-elements/evan.js diff --git a/top-k-frequent-elements/evan.js b/top-k-frequent-elements/evan.js new file mode 100644 index 000000000..cf5a2e879 --- /dev/null +++ b/top-k-frequent-elements/evan.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function (nums, k) { + const counter = new Map(); + + nums.forEach((num) => { + if (counter.has(num)) { + counter.set(num, counter.get(num) + 1); + } else { + counter.set(num, 1); + } + }); + + const sorted = [...counter.entries()].sort( + ([, freqA], [, freqB]) => freqB - freqA + ); + + return sorted.slice(0, k).map(([num]) => num); +}; From af6c80a251b358f13e4569a64258e2bd62e1da04 Mon Sep 17 00:00:00 2001 From: sounmind Date: Wed, 29 May 2024 16:00:14 -0400 Subject: [PATCH 3/6] solve: product of array except self --- product-of-array-except-self/evan.js | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 product-of-array-except-self/evan.js diff --git a/product-of-array-except-self/evan.js b/product-of-array-except-self/evan.js new file mode 100644 index 000000000..ee8289f9d --- /dev/null +++ b/product-of-array-except-self/evan.js @@ -0,0 +1,33 @@ +/** + * @param {number[]} nums + * @return {number[]} + */ +var productExceptSelf = function (nums) { + const length = nums.length; + const result = Array(length).fill(1); + + let leftProduct = 1; + for (let i = 0; i < length; i++) { + result[i] = leftProduct; + leftProduct *= nums[i]; + } + + let rightProduct = 1; + for (let i = length - 1; i >= 0; i--) { + result[i] *= rightProduct; + rightProduct *= nums[i]; + } + + return result; +}; + +/** + * Time Complexity: O(n) + * The algorithm iterates through the nums array twice (two separate loops), each taking O(n) time. + * Hence, the overall time complexity is O(2n), which simplifies to O(n). + * + * Space Complexity: O(1) + * The algorithm uses a constant amount of extra space for the leftProduct and rightProduct variables. + * The result array is not considered extra space as it is required for the output. + * Therefore, the extra space complexity is O(1). + */ From 7b021d098830e32d4ce8b7e2d69010cbcbb4d540 Mon Sep 17 00:00:00 2001 From: sounmind Date: Wed, 29 May 2024 16:04:05 -0400 Subject: [PATCH 4/6] docs: add complexities --- 3sum/evan.js | 11 +++++++++++ top-k-frequent-elements/evan.js | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/3sum/evan.js b/3sum/evan.js index cf97534bc..72530da0c 100644 --- a/3sum/evan.js +++ b/3sum/evan.js @@ -47,3 +47,14 @@ var threeSum = function (nums) { return result; }; + +/** + * Time Complexity: O(n^2) + * The algorithm involves sorting the input array, which takes O(n log n) time. + * The main part of the algorithm consists of a loop that runs O(n) times, and within that loop, there is a two-pointer technique that runs in O(n) time. + * Thus, the overall time complexity is O(n log n) + O(n^2), which simplifies to O(n^2). + * + * Space Complexity: O(n) + * The space complexity is O(n) due to the space needed for the sorted array and the result array. + * Although the sorting algorithm may require additional space, typically O(log n) for the in-place sort in JavaScript, the dominant term is O(n) for the result storage. + */ diff --git a/top-k-frequent-elements/evan.js b/top-k-frequent-elements/evan.js index cf5a2e879..7575cf866 100644 --- a/top-k-frequent-elements/evan.js +++ b/top-k-frequent-elements/evan.js @@ -20,3 +20,16 @@ var topKFrequent = function (nums, k) { return sorted.slice(0, k).map(([num]) => num); }; + +/** + * Time Complexity: O(n log n) + * - Counting the frequency of each element takes O(n) time. + * - Sorting the entries by frequency takes O(n log n) time. + * - Extracting the top k elements and mapping them takes O(k) time. + * - Therefore, the overall time complexity is dominated by the sorting step, resulting in O(n log n). + + * Space Complexity: O(n) + * - The counter map requires O(n) space to store the frequency of each element. + * - The sorted array also requires O(n) space. + * - Therefore, the overall space complexity is O(n). + */ From 0a44499a8f3e0239e2e5f371c3d4ffd4a793199a Mon Sep 17 00:00:00 2001 From: sounmind Date: Fri, 31 May 2024 15:37:25 -0400 Subject: [PATCH 5/6] solve: encode and decode strings --- encode-and-decode-strings/evan.js | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 encode-and-decode-strings/evan.js diff --git a/encode-and-decode-strings/evan.js b/encode-and-decode-strings/evan.js new file mode 100644 index 000000000..5a2ed3f5a --- /dev/null +++ b/encode-and-decode-strings/evan.js @@ -0,0 +1,58 @@ +const DELIMITER = "#"; + +/** + * + * @param {string[]} strs + * @returns {string} + */ +function encode(strs) { + return strs.map((s) => `${s.length}${DELIMITER}${s}`).join(""); +} + +/** + * + * @param {string} encodedStr + * @returns {string[]} + */ +function decode(encodedStr) { + const decodedStrings = []; + let currentIndex = 0; + + while (currentIndex < encodedStr.length) { + let delimiterIndex = currentIndex; + + while (encodedStr[delimiterIndex] !== DELIMITER) { + delimiterIndex += 1; + } + + const strLength = parseInt( + encodedStr.substring(currentIndex, delimiterIndex) + ); + + decodedStrings.push( + encodedStr.substring(delimiterIndex + 1, delimiterIndex + 1 + strLength) + ); + + currentIndex = delimiterIndex + 1 + strLength; + } + + return decodedStrings; +} +/** + * Time Complexity: O(n) where n is the length of the encoded string. + * Reason: + * The inner operations (finding # and extracting substrings) are proportional to the size of the encoded segments + * but are ultimately bounded by the total length of the input string. + * + * Space Complexity: O(k) where k is the total length of the decoded strings. + */ + +/** + * Test cases + */ +const strs4 = ["longestword", "short", "mid", "tiny"]; +const encoded4 = encode(strs4); +console.log(encoded4); // Output: "11#longestword5#short3#mid4#tiny" + +const decoded4 = decode(encoded4); +console.log(decoded4); // Output: ["longestword", "short", "mid", "tiny"] From f6347421059e529f26d4aac4aed6be8969cf09f1 Mon Sep 17 00:00:00 2001 From: sounmind Date: Fri, 31 May 2024 16:36:02 -0400 Subject: [PATCH 6/6] solve: longest consecutive sequence --- longest-consecutive-sequence/evan.js | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 longest-consecutive-sequence/evan.js diff --git a/longest-consecutive-sequence/evan.js b/longest-consecutive-sequence/evan.js new file mode 100644 index 000000000..0c9d155d1 --- /dev/null +++ b/longest-consecutive-sequence/evan.js @@ -0,0 +1,38 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var longestConsecutive = function (nums) { + const set = new Set(nums); + let longestStreak = 0; + + for (const num of set) { + // Check if it's the start of a sequence + if (!set.has(num - 1)) { + let currentNum = num; + let currentStreak = 1; + + // Find the length of the sequence + while (set.has(currentNum + 1)) { + currentNum += 1; + currentStreak += 1; + } + + longestStreak = Math.max(longestStreak, currentStreak); + } + } + + return longestStreak; +}; + +/** + * Time Complexity: O(n) where n is the length of the input array. + * Reason: + * Creating the Set: O(n) + * Iterating Through the Set: O(n) + * Checking for the Start of a Sequence: O(n) + * Finding the Length of the Sequence: O(n) across all sequences + * Tracking the Longest Sequence: O(n) + * + * Space Complexity: O(n) because of the additional space used by the set. + */