Skip to content

[Helena] Week 5 solutions #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 2, 2024
34 changes: 34 additions & 0 deletions 3sum/yolophg.js
Original file line number Diff line number Diff line change
@@ -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));
};
14 changes: 14 additions & 0 deletions encode-and-decode-strings/yolophg.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let solution = new Solution();
solution.decode(solution.encode(['hello', '|world']))

이렇게 하면 깰 수 있을 것 같아요!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 decode하면 빈 스트링이 하나 추가되겠군요!

Original file line number Diff line number Diff line change
@@ -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('|');
}
Comment on lines +5 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반칙? 🙈

}
31 changes: 31 additions & 0 deletions longest-consecutive-sequence/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
}
26 changes: 26 additions & 0 deletions product-of-array-except-self/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};
26 changes: 26 additions & 0 deletions top-k-frequent-elements/yolophg.js
Copy link
Member

@DaleSeo DaleSeo May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정렬이나 힙을 이용하지 않고, 배열의 인덱스에 빈도를 저장하디니, 굉장히 좋은 아이디어인 것 같습니다! 👍👍👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity: O(n)
// Space Complexity: O(n)

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) + 1);
}

// 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--) {
result.push(...buckets[i]);
}

// ensure the result length is k.
return result.slice(0, k);
};