From ded43f40053e73dfa783d9af946ee51c8d9be681 Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Mon, 27 May 2024 19:39:25 -0400 Subject: [PATCH 1/7] Add week 5 soultions : topKFrequentElements --- top-k-frequent-elements/yolophg.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 top-k-frequent-elements/yolophg.js diff --git a/top-k-frequent-elements/yolophg.js b/top-k-frequent-elements/yolophg.js new file mode 100644 index 000000000..9f7388a3c --- /dev/null +++ b/top-k-frequent-elements/yolophg.js @@ -0,0 +1,28 @@ +// Time Complexity: O(n) +// Space Complexity: O(m) + +var topKFrequent = function(nums, k) { + const frequencyMap = new Map(); + // for each number in the array, update frequency. + for (const num of nums) { + frequencyMap.set(num, (frequencyMap.get(num) || 0)); + } + + // create buckets where index represents frequency. + const buckets = Array(nums.length + 1).fill().map(() => []); + // place each number into the bucket corresponding to frequency. + for (const [num, frequency] of frequencyMap.entries()) { + buckets[frequency].push(num); + } + + const result = []; + // iterate from the highest possible frequency down to the lowest. + for (let i = buckets.length - 1; i >= 0 && result.length < k; i--) { + if (buckets[i].length > 0) { + result.push(...buckets[i]); + } + } + + // ensure the result length is k. + return result.slice(0, k); +}; From 6b66901c5cba6f3c17ff962617a1e820bbf6f561 Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Mon, 27 May 2024 19:39:55 -0400 Subject: [PATCH 2/7] Fix typo --- top-k-frequent-elements/yolophg.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/top-k-frequent-elements/yolophg.js b/top-k-frequent-elements/yolophg.js index 9f7388a3c..da00e6607 100644 --- a/top-k-frequent-elements/yolophg.js +++ b/top-k-frequent-elements/yolophg.js @@ -1,5 +1,5 @@ // Time Complexity: O(n) -// Space Complexity: O(m) +// Space Complexity: O(n) var topKFrequent = function(nums, k) { const frequencyMap = new Map(); From 7110331a92b60217eff2bf7e4a3ed6cbfcc8ea60 Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Tue, 28 May 2024 00:01:29 -0400 Subject: [PATCH 3/7] Fix mistake in line 8 --- top-k-frequent-elements/yolophg.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/top-k-frequent-elements/yolophg.js b/top-k-frequent-elements/yolophg.js index da00e6607..97f1b7be1 100644 --- a/top-k-frequent-elements/yolophg.js +++ b/top-k-frequent-elements/yolophg.js @@ -5,7 +5,7 @@ var topKFrequent = function(nums, k) { const frequencyMap = new Map(); // for each number in the array, update frequency. for (const num of nums) { - frequencyMap.set(num, (frequencyMap.get(num) || 0)); + frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1); } // create buckets where index represents frequency. From ba05d0ebf9e924f5ebbc9d0ac0e09031b346becc Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Tue, 28 May 2024 00:04:04 -0400 Subject: [PATCH 4/7] Remove unnecessary condition --- top-k-frequent-elements/yolophg.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/top-k-frequent-elements/yolophg.js b/top-k-frequent-elements/yolophg.js index 97f1b7be1..84b0373cd 100644 --- a/top-k-frequent-elements/yolophg.js +++ b/top-k-frequent-elements/yolophg.js @@ -18,9 +18,7 @@ var topKFrequent = function(nums, k) { const result = []; // iterate from the highest possible frequency down to the lowest. for (let i = buckets.length - 1; i >= 0 && result.length < k; i--) { - if (buckets[i].length > 0) { - result.push(...buckets[i]); - } + result.push(...buckets[i]); } // ensure the result length is k. From 6230c4a8762e4b17c317a6640d19ba6170a307ae Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Tue, 28 May 2024 14:40:45 -0400 Subject: [PATCH 5/7] Add week 5 soultions : encodeAndDecodeStrings --- encode-and-decode-strings/yolophg.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 encode-and-decode-strings/yolophg.js diff --git a/encode-and-decode-strings/yolophg.js b/encode-and-decode-strings/yolophg.js new file mode 100644 index 000000000..d8c1f263d --- /dev/null +++ b/encode-and-decode-strings/yolophg.js @@ -0,0 +1,14 @@ +// Time Complexity: O(n), O(n) +// Space Complexity: O(n), O(n) + +class Solution { + encode(strs) { + // join the array into a single string using a delimiter, '|'. + return strs.join('|'); + } + + decode(str) { + // split the string using the delimiter '|' and return the array. + return str.split('|'); + } +} From 5ac4e427fa2b9bbeec600696fe55cef06d1d08ca Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Thu, 30 May 2024 11:08:49 -0400 Subject: [PATCH 6/7] Add week 5 soultions : productOfArrayExceptSelf --- product-of-array-except-self/yolophg.js | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 product-of-array-except-self/yolophg.js diff --git a/product-of-array-except-self/yolophg.js b/product-of-array-except-self/yolophg.js new file mode 100644 index 000000000..044038dc0 --- /dev/null +++ b/product-of-array-except-self/yolophg.js @@ -0,0 +1,26 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) + +var productExceptSelf = function(nums) { + const n = nums.length; + const leftProducts = new Array(n).fill(1); + const rightProducts = new Array(n).fill(1); + const result = new Array(n); + + // leftProduct will be the product of all elements to the left of index i. + for (let i = 1; i < n; i++) { + leftProducts[i] = leftProducts[i - 1] * nums[i - 1]; + } + + // rightProduct will be the product of all elements to the right of index i. + for (let i = n - 2; i >= 0; i--) { + rightProducts[i] = rightProducts[i + 1] * nums[i + 1]; + } + + // result will be the product of leftProduct and rightProduct. + for (let i = 0; i < n; i++) { + result[i] = leftProducts[i] * rightProducts[i]; + } + + return result; +}; From e3512181eb70122bd2116e9184cc9f7e64ed9a58 Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Sun, 2 Jun 2024 13:57:11 -0400 Subject: [PATCH 7/7] Add week 5 solutions : longestConsecutiveSequence, 3Sum --- 3sum/yolophg.js | 34 +++++++++++++++++++++++++ longest-consecutive-sequence/yolophg.js | 31 ++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 3sum/yolophg.js create mode 100644 longest-consecutive-sequence/yolophg.js diff --git a/3sum/yolophg.js b/3sum/yolophg.js new file mode 100644 index 000000000..ad8cea63d --- /dev/null +++ b/3sum/yolophg.js @@ -0,0 +1,34 @@ +// Time Complexity: O(n^2) +// Space Complexity: O(n) + +// Time Complexity: O(n^2) +// Space Complexity: O(n) + +var threeSum = function(nums) { + nums.sort((a, b) => a - b); + // create a set to store triplets. + const result = new Set(); + + // loop through the array, but stop 2 elements before the end. + for (let i = 0; i < nums.length - 2; i++) { + // if the current element is the same as the one before it, skip it to avoid duplicates. + if (i > 0 && nums[i] === nums[i - 1]) continue; + + // create a set to keep track of the complements. + const complements = new Set(); + // start another loop from the next element. + for (let j = i + 1; j < nums.length; j++) { + const complement = -nums[i] - nums[j]; + // check if the current number is in the set. + if (complements.has(nums[j])) { + // if it is, found a triplet. Add it to the result set as a sorted string to avoid duplicates. + result.add(JSON.stringify([nums[i], complement, nums[j]].sort((a, b) => a - b))); + } else { + complements.add(complement); + } + } + } + + // convert set of strings back to arrays. + return Array.from(result).map(triplet => JSON.parse(triplet)); +}; diff --git a/longest-consecutive-sequence/yolophg.js b/longest-consecutive-sequence/yolophg.js new file mode 100644 index 000000000..55da4a2bb --- /dev/null +++ b/longest-consecutive-sequence/yolophg.js @@ -0,0 +1,31 @@ +// Time Complexity: O(n log n) +// Space Complexity: O(n) + +var longestConsecutive = function(nums) { + if (nums.length === 0) return 0; + + nums.sort((a, b) => a - b); + + let longestStreak = 1; + let currentStreak = 1; + + // iterate through the sorted array. + for (let i = 1; i < nums.length; i++) { + // check for duplicates. + if (nums[i] !== nums[i - 1]) { + if (nums[i] === nums[i - 1] + 1) { + // if current element is consecutive, increment current streak. + currentStreak++; + } else { + // if not, update longest streak and reset current streak. + longestStreak = Math.max(longestStreak, currentStreak); + currentStreak = 1; + } + } + } + + // final comparison to ensure the longest streak is captured. + longestStreak = Math.max(longestStreak, currentStreak); + + return longestStreak; +}