-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ded43f4
Add week 5 soultions : topKFrequentElements
yolophg 6b66901
Fix typo
yolophg fba8796
Merge branch 'DaleStudy:main' into main
yolophg 7110331
Fix mistake in line 8
yolophg ba05d0e
Remove unnecessary condition
yolophg 6230c4a
Add week 5 soultions : encodeAndDecodeStrings
yolophg be5d63c
Merge branch 'DaleStudy:main' into main
yolophg 5ac4e42
Add week 5 soultions : productOfArrayExceptSelf
yolophg ebb5f61
Merge branch 'DaleStudy:main' into main
yolophg e44814c
Merge branch 'DaleStudy:main' into main
yolophg e351218
Add week 5 solutions : longestConsecutiveSequence, 3Sum
yolophg 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,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)); | ||
}; |
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,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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반칙? 🙈 |
||
} |
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,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; | ||
} |
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,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; | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정렬이나 힙을 이용하지 않고, 배열의 인덱스에 빈도를 저장하디니, 굉장히 좋은 아이디어인 것 같습니다! 👍👍👍 |
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,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); | ||
}; |
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.
이렇게 하면 깰 수 있을 것 같아요!
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.
오 decode하면 빈 스트링이 하나 추가되겠군요!