Skip to content

Commit 27dbd40

Browse files
authored
Merge pull request #98 from nhistory/week5
[Sehwan]Week5 solutions with javascript
2 parents 9ee7b25 + 6686b74 commit 27dbd40

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

3sum/nhistory.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var threeSum = function (nums) {
2+
// Sort nums array
3+
const sortedNums = nums.sort((a, b) => a - b);
4+
let result = [];
5+
6+
// Start iteration to pick first element for 3sum
7+
for (let i = 0; i < sortedNums.length; i++) {
8+
// Check if the first element is already greater than 0, no valid triplets possible after this point
9+
if (sortedNums[i] > 0) {
10+
break;
11+
}
12+
13+
// Skip duplicates of the first element to avoid redundant triplets
14+
if (i > 0 && sortedNums[i] === sortedNums[i - 1]) {
15+
continue;
16+
}
17+
18+
// Iterate to find sum of two pointer and nums[i]
19+
let left = i + 1;
20+
let right = sortedNums.length - 1;
21+
22+
while (left < right) {
23+
let sum = sortedNums[i] + sortedNums[left] + sortedNums[right];
24+
25+
if (sum === 0) {
26+
result.push([sortedNums[i], sortedNums[left], sortedNums[right]]);
27+
// Skip duplicates of left and right pointers to avoid redundant triplets
28+
while (sortedNums[left] === sortedNums[left + 1]) left++;
29+
while (sortedNums[right] === sortedNums[right - 1]) right--;
30+
left++;
31+
right--;
32+
} else if (sum < 0) {
33+
left++;
34+
} else if (sum > 0) {
35+
right--;
36+
}
37+
}
38+
}
39+
return result;
40+
};
41+
42+
// TC: O(n^2)
43+
// SC: O(n)

encode-and-decode-strings/nhistory.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
/**
3+
* @param {string[]} strs
4+
* @returns {string}
5+
*/
6+
encode(strs) {
7+
let result = "";
8+
for (let str of strs) {
9+
result += str.length.toString() + "#" + str;
10+
}
11+
return result;
12+
}
13+
14+
/**
15+
* @param {string} str
16+
* @returns {string[]}
17+
*/
18+
decode(str) {
19+
let result = [];
20+
let i = 0;
21+
22+
while (i < str.length) {
23+
// Find the position of the next '#'
24+
let j = i;
25+
while (str[j] !== "#") {
26+
j++;
27+
}
28+
29+
// Length of the next string
30+
const len = parseInt(str.slice(i, j));
31+
i = j + 1; // Move past the '#'
32+
33+
// Extract the string of length 'len'
34+
result.push(str.slice(i, i + len));
35+
i += len; // Move past the extracted string
36+
}
37+
38+
return result;
39+
}
40+
}
41+
42+
// TC: O(n),O(n)
43+
// SC: O(n),O(n)
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var longestConsecutive = function (nums) {
2+
// Return 0 if there are no elements in nums
3+
if (nums.length === 0) return 0;
4+
5+
// Create a set to find values efficiently
6+
const numSet = new Set(nums);
7+
let maxLength = 0;
8+
9+
for (let num of numSet) {
10+
// Check if this is the start of a sequence
11+
if (!numSet.has(num - 1)) {
12+
let currentNum = num;
13+
let currentLength = 1;
14+
15+
// Count the length of the sequence
16+
while (numSet.has(currentNum + 1)) {
17+
currentNum += 1;
18+
currentLength += 1;
19+
}
20+
21+
// Update the maximum length
22+
maxLength = Math.max(maxLength, currentLength);
23+
}
24+
}
25+
26+
return maxLength;
27+
};
28+
29+
// TC: O(n)
30+
// SC: O(n)
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var productExceptSelf = function (nums) {
6+
const len = nums.length;
7+
const lastIndex = len - 1;
8+
9+
// Declare prefix, postfix array
10+
let prefix = new Array(len).fill(1);
11+
let postfix = new Array(len).fill(1);
12+
13+
// Iterate loop to add prefix[i-1]*nums[i] values into prefix array
14+
prefix[0] = nums[0];
15+
for (let i = 1; i < nums.length; i++) {
16+
prefix[i] = prefix[i - 1] * nums[i];
17+
}
18+
19+
// Iterate loop to add postfix[i]*nums[i-1] values into postfix array
20+
postfix[lastIndex] = nums[lastIndex];
21+
for (let i = lastIndex; i >= 1; i--) {
22+
postfix[i - 1] = postfix[i] * nums[i - 1];
23+
}
24+
25+
// Make output array with prefix and postfix arrays
26+
let output = new Array(len).fill(1);
27+
28+
// First index value is equal to postfix[1]
29+
output[0] = postfix[1];
30+
// Last index value is equal to prefix[lastIndex-1]
31+
output[lastIndex] = prefix[lastIndex - 1];
32+
for (let i = 1; i < len - 1; i++) {
33+
output[i] = prefix[i - 1] * postfix[i + 1];
34+
}
35+
36+
return output;
37+
};
38+
39+
// TC: O(n)
40+
// SC: O(n)

top-k-frequent-elements/nhistory.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var topKFrequent = function (nums, k) {
2+
// 1. Hashmap { num : frequency }
3+
let map = {};
4+
5+
// 2. Iterate counts frequency of each value
6+
for (num of nums) {
7+
// Check there is already num key or not
8+
if (!map[num]) map[num] = 0;
9+
map[num]++;
10+
}
11+
12+
// 3. Sort frequency and return sliced array
13+
return [...Object.keys(map)].sort((a, b) => map[b] - map[a]).slice(0, k);
14+
};

0 commit comments

Comments
 (0)