-
-
Notifications
You must be signed in to change notification settings - Fork 195
Evan: Week 5 #101
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
Evan: Week 5 #101
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
44eaaa6
solve: 3sum
sounmind 35b3b15
solve: top K frequent elements
sounmind af6c80a
solve: product of array except self
sounmind 7b021d0
docs: add complexities
sounmind 0a44499
solve: encode and decode strings
sounmind f634742
solve: longest consecutive sequence
sounmind 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,60 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
*/ | ||
var threeSum = function (nums) { | ||
const sorted = nums.sort((a, b) => a - b); | ||
const result = []; | ||
|
||
for (let i = 0; i < sorted.length; i++) { | ||
const fixedNumber = sorted[i]; | ||
const previousFixedNumber = sorted[i - 1]; | ||
|
||
if (fixedNumber === previousFixedNumber) { | ||
continue; | ||
} | ||
|
||
let [leftEnd, rightEnd] = [i + 1, sorted.length - 1]; | ||
|
||
while (leftEnd < rightEnd) { | ||
const sum = fixedNumber + sorted[leftEnd] + sorted[rightEnd]; | ||
|
||
if (sum === 0) { | ||
result.push([sorted[leftEnd], sorted[rightEnd], sorted[i]]); | ||
|
||
while ( | ||
sorted[leftEnd + 1] === sorted[leftEnd] || | ||
sorted[rightEnd - 1] === sorted[rightEnd] | ||
) { | ||
if (sorted[leftEnd + 1] === sorted[leftEnd]) { | ||
leftEnd += 1; | ||
} | ||
|
||
if (sorted[rightEnd - 1] === sorted[rightEnd]) { | ||
rightEnd -= 1; | ||
} | ||
} | ||
|
||
leftEnd += 1; | ||
rightEnd -= 1; | ||
} else if (sum < 0) { | ||
leftEnd += 1; | ||
} else { | ||
rightEnd -= 1; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
/** | ||
* Time Complexity: O(n^2) | ||
* The algorithm involves sorting the input array, which takes O(n log n) time. | ||
* The main part of the algorithm consists of a loop that runs O(n) times, and within that loop, there is a two-pointer technique that runs in O(n) time. | ||
* Thus, the overall time complexity is O(n log n) + O(n^2), which simplifies to O(n^2). | ||
* | ||
* Space Complexity: O(n) | ||
* The space complexity is O(n) due to the space needed for the sorted array and the result array. | ||
* Although the sorting algorithm may require additional space, typically O(log n) for the in-place sort in JavaScript, the dominant term is O(n) for the result storage. | ||
*/ |
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,58 @@ | ||
const DELIMITER = "#"; | ||
|
||
/** | ||
* | ||
* @param {string[]} strs | ||
* @returns {string} | ||
*/ | ||
function encode(strs) { | ||
return strs.map((s) => `${s.length}${DELIMITER}${s}`).join(""); | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} encodedStr | ||
* @returns {string[]} | ||
*/ | ||
function decode(encodedStr) { | ||
const decodedStrings = []; | ||
let currentIndex = 0; | ||
|
||
while (currentIndex < encodedStr.length) { | ||
let delimiterIndex = currentIndex; | ||
|
||
while (encodedStr[delimiterIndex] !== DELIMITER) { | ||
delimiterIndex += 1; | ||
} | ||
|
||
const strLength = parseInt( | ||
encodedStr.substring(currentIndex, delimiterIndex) | ||
); | ||
|
||
decodedStrings.push( | ||
encodedStr.substring(delimiterIndex + 1, delimiterIndex + 1 + strLength) | ||
); | ||
|
||
currentIndex = delimiterIndex + 1 + strLength; | ||
} | ||
|
||
return decodedStrings; | ||
} | ||
/** | ||
* Time Complexity: O(n) where n is the length of the encoded string. | ||
* Reason: | ||
* The inner operations (finding # and extracting substrings) are proportional to the size of the encoded segments | ||
* but are ultimately bounded by the total length of the input string. | ||
* | ||
* Space Complexity: O(k) where k is the total length of the decoded strings. | ||
*/ | ||
|
||
/** | ||
* Test cases | ||
*/ | ||
const strs4 = ["longestword", "short", "mid", "tiny"]; | ||
const encoded4 = encode(strs4); | ||
console.log(encoded4); // Output: "11#longestword5#short3#mid4#tiny" | ||
|
||
const decoded4 = decode(encoded4); | ||
console.log(decoded4); // Output: ["longestword", "short", "mid", "tiny"] |
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,38 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = function (nums) { | ||
const set = new Set(nums); | ||
let longestStreak = 0; | ||
|
||
for (const num of set) { | ||
// Check if it's the start of a sequence | ||
if (!set.has(num - 1)) { | ||
let currentNum = num; | ||
let currentStreak = 1; | ||
|
||
// Find the length of the sequence | ||
while (set.has(currentNum + 1)) { | ||
currentNum += 1; | ||
currentStreak += 1; | ||
} | ||
|
||
longestStreak = Math.max(longestStreak, currentStreak); | ||
} | ||
} | ||
|
||
return longestStreak; | ||
}; | ||
|
||
/** | ||
* Time Complexity: O(n) where n is the length of the input array. | ||
* Reason: | ||
* Creating the Set: O(n) | ||
* Iterating Through the Set: O(n) | ||
* Checking for the Start of a Sequence: O(n) | ||
* Finding the Length of the Sequence: O(n) across all sequences | ||
* Tracking the Longest Sequence: O(n) | ||
* | ||
* Space Complexity: O(n) because of the additional space used by the set. | ||
*/ |
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,33 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
*/ | ||
var productExceptSelf = function (nums) { | ||
const length = nums.length; | ||
const result = Array(length).fill(1); | ||
|
||
let leftProduct = 1; | ||
for (let i = 0; i < length; i++) { | ||
result[i] = leftProduct; | ||
leftProduct *= nums[i]; | ||
} | ||
|
||
let rightProduct = 1; | ||
for (let i = length - 1; i >= 0; i--) { | ||
result[i] *= rightProduct; | ||
rightProduct *= nums[i]; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
/** | ||
* Time Complexity: O(n) | ||
* The algorithm iterates through the nums array twice (two separate loops), each taking O(n) time. | ||
* Hence, the overall time complexity is O(2n), which simplifies to O(n). | ||
* | ||
* Space Complexity: O(1) | ||
* The algorithm uses a constant amount of extra space for the leftProduct and rightProduct variables. | ||
* The result array is not considered extra space as it is required for the output. | ||
* Therefore, the extra space complexity is O(1). | ||
*/ |
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,35 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
var topKFrequent = function (nums, k) { | ||
const counter = new Map(); | ||
|
||
nums.forEach((num) => { | ||
if (counter.has(num)) { | ||
counter.set(num, counter.get(num) + 1); | ||
} else { | ||
counter.set(num, 1); | ||
} | ||
}); | ||
|
||
const sorted = [...counter.entries()].sort( | ||
([, freqA], [, freqB]) => freqB - freqA | ||
); | ||
|
||
return sorted.slice(0, k).map(([num]) => num); | ||
}; | ||
|
||
/** | ||
* Time Complexity: O(n log n) | ||
* - Counting the frequency of each element takes O(n) time. | ||
* - Sorting the entries by frequency takes O(n log n) time. | ||
* - Extracting the top k elements and mapping them takes O(k) time. | ||
* - Therefore, the overall time complexity is dominated by the sorting step, resulting in O(n log n). | ||
|
||
* Space Complexity: O(n) | ||
* - The counter map requires O(n) space to store the frequency of each element. | ||
* - The sorted array also requires O(n) space. | ||
* - Therefore, the overall space complexity is O(n). | ||
*/ |
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.
μ΄λ κ² λ³μλͺ μ μ°λκΉ λ λͺ ννκ² μ½λκ° μ½νλ€μ!