Skip to content

Commit

Permalink
Merge pull request #98 from nhistory/week5
Browse files Browse the repository at this point in the history
[Sehwan]Week5 solutions with javascript
  • Loading branch information
DaleSeo authored May 31, 2024
2 parents 9ee7b25 + 6686b74 commit 27dbd40
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 3sum/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var threeSum = function (nums) {
// Sort nums array
const sortedNums = nums.sort((a, b) => a - b);
let result = [];

// Start iteration to pick first element for 3sum
for (let i = 0; i < sortedNums.length; i++) {
// Check if the first element is already greater than 0, no valid triplets possible after this point
if (sortedNums[i] > 0) {
break;
}

// Skip duplicates of the first element to avoid redundant triplets
if (i > 0 && sortedNums[i] === sortedNums[i - 1]) {
continue;
}

// Iterate to find sum of two pointer and nums[i]
let left = i + 1;
let right = sortedNums.length - 1;

while (left < right) {
let sum = sortedNums[i] + sortedNums[left] + sortedNums[right];

if (sum === 0) {
result.push([sortedNums[i], sortedNums[left], sortedNums[right]]);
// Skip duplicates of left and right pointers to avoid redundant triplets
while (sortedNums[left] === sortedNums[left + 1]) left++;
while (sortedNums[right] === sortedNums[right - 1]) right--;
left++;
right--;
} else if (sum < 0) {
left++;
} else if (sum > 0) {
right--;
}
}
}
return result;
};

// TC: O(n^2)
// SC: O(n)
43 changes: 43 additions & 0 deletions encode-and-decode-strings/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
/**
* @param {string[]} strs
* @returns {string}
*/
encode(strs) {
let result = "";
for (let str of strs) {
result += str.length.toString() + "#" + str;
}
return result;
}

/**
* @param {string} str
* @returns {string[]}
*/
decode(str) {
let result = [];
let i = 0;

while (i < str.length) {
// Find the position of the next '#'
let j = i;
while (str[j] !== "#") {
j++;
}

// Length of the next string
const len = parseInt(str.slice(i, j));
i = j + 1; // Move past the '#'

// Extract the string of length 'len'
result.push(str.slice(i, i + len));
i += len; // Move past the extracted string
}

return result;
}
}

// TC: O(n),O(n)
// SC: O(n),O(n)
30 changes: 30 additions & 0 deletions longest-consecutive-sequence/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var longestConsecutive = function (nums) {
// Return 0 if there are no elements in nums
if (nums.length === 0) return 0;

// Create a set to find values efficiently
const numSet = new Set(nums);
let maxLength = 0;

for (let num of numSet) {
// Check if this is the start of a sequence
if (!numSet.has(num - 1)) {
let currentNum = num;
let currentLength = 1;

// Count the length of the sequence
while (numSet.has(currentNum + 1)) {
currentNum += 1;
currentLength += 1;
}

// Update the maximum length
maxLength = Math.max(maxLength, currentLength);
}
}

return maxLength;
};

// TC: O(n)
// SC: O(n)
40 changes: 40 additions & 0 deletions product-of-array-except-self/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function (nums) {
const len = nums.length;
const lastIndex = len - 1;

// Declare prefix, postfix array
let prefix = new Array(len).fill(1);
let postfix = new Array(len).fill(1);

// Iterate loop to add prefix[i-1]*nums[i] values into prefix array
prefix[0] = nums[0];
for (let i = 1; i < nums.length; i++) {
prefix[i] = prefix[i - 1] * nums[i];
}

// Iterate loop to add postfix[i]*nums[i-1] values into postfix array
postfix[lastIndex] = nums[lastIndex];
for (let i = lastIndex; i >= 1; i--) {
postfix[i - 1] = postfix[i] * nums[i - 1];
}

// Make output array with prefix and postfix arrays
let output = new Array(len).fill(1);

// First index value is equal to postfix[1]
output[0] = postfix[1];
// Last index value is equal to prefix[lastIndex-1]
output[lastIndex] = prefix[lastIndex - 1];
for (let i = 1; i < len - 1; i++) {
output[i] = prefix[i - 1] * postfix[i + 1];
}

return output;
};

// TC: O(n)
// SC: O(n)
14 changes: 14 additions & 0 deletions top-k-frequent-elements/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var topKFrequent = function (nums, k) {
// 1. Hashmap { num : frequency }
let map = {};

// 2. Iterate counts frequency of each value
for (num of nums) {
// Check there is already num key or not
if (!map[num]) map[num] = 0;
map[num]++;
}

// 3. Sort frequency and return sliced array
return [...Object.keys(map)].sort((a, b) => map[b] - map[a]).slice(0, k);
};

0 comments on commit 27dbd40

Please sign in to comment.