-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
// Space complexity: O(n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
알고리즘 개선 작업을 계획하고 계신 것 같은데, 현재 답안도 충분히 훌륭한 것 같아서 PR은 승인해드리도록 하겠습니다. 승인 후에 추가 커밋을 푸시하셔도 승인이 취소되거나 하지는 않으니 자유롭게 추가 작업하시기 바랍니다.