Skip to content

Commit 3e99718

Browse files
authored
Merge pull request #100 from yolophg/main
[Helena] Week 5 solutions
2 parents c9fe566 + e351218 commit 3e99718

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

3sum/yolophg.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Time Complexity: O(n^2)
2+
// Space Complexity: O(n)
3+
4+
// Time Complexity: O(n^2)
5+
// Space Complexity: O(n)
6+
7+
var threeSum = function(nums) {
8+
nums.sort((a, b) => a - b);
9+
// create a set to store triplets.
10+
const result = new Set();
11+
12+
// loop through the array, but stop 2 elements before the end.
13+
for (let i = 0; i < nums.length - 2; i++) {
14+
// if the current element is the same as the one before it, skip it to avoid duplicates.
15+
if (i > 0 && nums[i] === nums[i - 1]) continue;
16+
17+
// create a set to keep track of the complements.
18+
const complements = new Set();
19+
// start another loop from the next element.
20+
for (let j = i + 1; j < nums.length; j++) {
21+
const complement = -nums[i] - nums[j];
22+
// check if the current number is in the set.
23+
if (complements.has(nums[j])) {
24+
// if it is, found a triplet. Add it to the result set as a sorted string to avoid duplicates.
25+
result.add(JSON.stringify([nums[i], complement, nums[j]].sort((a, b) => a - b)));
26+
} else {
27+
complements.add(complement);
28+
}
29+
}
30+
}
31+
32+
// convert set of strings back to arrays.
33+
return Array.from(result).map(triplet => JSON.parse(triplet));
34+
};

encode-and-decode-strings/yolophg.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Time Complexity: O(n), O(n)
2+
// Space Complexity: O(n), O(n)
3+
4+
class Solution {
5+
encode(strs) {
6+
// join the array into a single string using a delimiter, '|'.
7+
return strs.join('|');
8+
}
9+
10+
decode(str) {
11+
// split the string using the delimiter '|' and return the array.
12+
return str.split('|');
13+
}
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Time Complexity: O(n log n)
2+
// Space Complexity: O(n)
3+
4+
var longestConsecutive = function(nums) {
5+
if (nums.length === 0) return 0;
6+
7+
nums.sort((a, b) => a - b);
8+
9+
let longestStreak = 1;
10+
let currentStreak = 1;
11+
12+
// iterate through the sorted array.
13+
for (let i = 1; i < nums.length; i++) {
14+
// check for duplicates.
15+
if (nums[i] !== nums[i - 1]) {
16+
if (nums[i] === nums[i - 1] + 1) {
17+
// if current element is consecutive, increment current streak.
18+
currentStreak++;
19+
} else {
20+
// if not, update longest streak and reset current streak.
21+
longestStreak = Math.max(longestStreak, currentStreak);
22+
currentStreak = 1;
23+
}
24+
}
25+
}
26+
27+
// final comparison to ensure the longest streak is captured.
28+
longestStreak = Math.max(longestStreak, currentStreak);
29+
30+
return longestStreak;
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
var productExceptSelf = function(nums) {
5+
const n = nums.length;
6+
const leftProducts = new Array(n).fill(1);
7+
const rightProducts = new Array(n).fill(1);
8+
const result = new Array(n);
9+
10+
// leftProduct will be the product of all elements to the left of index i.
11+
for (let i = 1; i < n; i++) {
12+
leftProducts[i] = leftProducts[i - 1] * nums[i - 1];
13+
}
14+
15+
// rightProduct will be the product of all elements to the right of index i.
16+
for (let i = n - 2; i >= 0; i--) {
17+
rightProducts[i] = rightProducts[i + 1] * nums[i + 1];
18+
}
19+
20+
// result will be the product of leftProduct and rightProduct.
21+
for (let i = 0; i < n; i++) {
22+
result[i] = leftProducts[i] * rightProducts[i];
23+
}
24+
25+
return result;
26+
};

top-k-frequent-elements/yolophg.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
var topKFrequent = function(nums, k) {
5+
const frequencyMap = new Map();
6+
// for each number in the array, update frequency.
7+
for (const num of nums) {
8+
frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1);
9+
}
10+
11+
// create buckets where index represents frequency.
12+
const buckets = Array(nums.length + 1).fill().map(() => []);
13+
// place each number into the bucket corresponding to frequency.
14+
for (const [num, frequency] of frequencyMap.entries()) {
15+
buckets[frequency].push(num);
16+
}
17+
18+
const result = [];
19+
// iterate from the highest possible frequency down to the lowest.
20+
for (let i = buckets.length - 1; i >= 0 && result.length < k; i--) {
21+
result.push(...buckets[i]);
22+
}
23+
24+
// ensure the result length is k.
25+
return result.slice(0, k);
26+
};

0 commit comments

Comments
 (0)