Skip to content

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 6 commits into from
Jun 1, 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
60 changes: 60 additions & 0 deletions 3sum/evan.js
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.
*/
58 changes: 58 additions & 0 deletions encode-and-decode-strings/evan.js
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"]
38 changes: 38 additions & 0 deletions longest-consecutive-sequence/evan.js
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.
*/
33 changes: 33 additions & 0 deletions product-of-array-except-self/evan.js
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).
*/
35 changes: 35 additions & 0 deletions top-k-frequent-elements/evan.js
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
Copy link
Contributor

Choose a reason for hiding this comment

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

μ΄λ ‡κ²Œ λ³€μˆ˜λͺ…을 μ“°λ‹ˆκΉŒ 더 λͺ…ν™•ν•˜κ²Œ μ½”λ“œκ°€ μ½νžˆλ„€μš”!

);

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).
*/