Skip to content

[yoonthecoder] Week 1 #635

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 4 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions contains-duplicate/yoonthecoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var containsDuplicate = function (nums) {
// Create a set from the nums array. Since Sets only allow unique values, any duplicates will be removed.
const set = new Set(nums);
// Compare the size of the set and the length of the original array.- if the size of the set is smaller than the length of the original array('nums'), it means there were duplicates.

return set.size < nums.length;
};

// Time Complexity: O(n); - adding elements to the Set & compare sizes
// Space Complexity: O(n)
21 changes: 21 additions & 0 deletions longest-consecutive-sequence/yoonthecoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var longestConsecutive = function (nums) {
// remove the duplicates from the array and sort it in an ascending order.
const setArray = [...new Set(nums)];
const sortedArray = setArray.sort((a, b) => a - b);
// create a set to store streak lengths, even when count resets.
const countSet = new Set();
let count = 0;
for (let i = 0; i < sortedArray.length; i++) {
if (sortedArray[i] + 1 == sortedArray[i + 1]) {
count += 1;
countSet.add(count);
} else {
count = 0;
}
}

return nums.length === 0 ? 0 : countSet.size + 1;
};

// Time complexity: O(nlogn) => TODO: need to improve this to O(n)
Copy link
Member

Choose a reason for hiding this comment

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

알고리즘 개선 작업을 계획하고 계신 것 같은데, 현재 답안도 충분히 훌륭한 것 같아서 PR은 승인해드리도록 하겠습니다. 승인 후에 추가 커밋을 푸시하셔도 승인이 취소되거나 하지는 않으니 자유롭게 추가 작업하시기 바랍니다.

// Space complexity: O(n)
25 changes: 25 additions & 0 deletions top-k-frequent-elements/yoonthecoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var topKFrequent = function (nums, k) {
// 1. count the frequency of each number in the array
const map = new Map();

// iterating through the array to count how many times each num appears.
for (const num of nums) {
// if the num already exists in the map, increment its count
if (map.has(num)) {
map.set(num, map.get(num) + 1);
} // otherwise, set it to 1
else map.set(num, 1);
}

// 2.create an array to store the freqeuncy numbers
const freqArr = [];
for (const [num, freq] of map) {
freqArr.push([num, freq]);
}
// sort in descending order by frequency
freqArr.sort((a, b) => b[1] - a[1]);
return freqArr.slice(0, k).map(([num]) => num);
};

// Time complexity: O(nlogn)
// Space complexity: O(n)
11 changes: 11 additions & 0 deletions valid-palindrome/yoonthecoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var isPalindrome = function (s) {
// remove any special characters and space from the string
const formattedString = s.toLowerCase().replace(/[^a-zA-Z0-9]/g, '');
// use split() method to separate each charaters and put them in an array - reverse it - concatenate
const reversedString = formattedString.split('').reverse().join('');

return reversedString === formattedString;
};

// time complexity: O(n)
// space complexity: O(n)
Loading