Skip to content

Commit ee59d0d

Browse files
authoredSep 6, 2024
Merge pull request #413 from kim-young/main
[kimyoung] WEEK 04 Solutions
2 parents 1b9c9e6 + 7a2d509 commit ee59d0d

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed
 
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var longestConsecutive = function (nums) { // sorting approach
2+
if (!nums.length) return 0;
3+
let set = new Set(nums);
4+
let sorted = [...set].sort((a, b) => a - b);
5+
let longestSeq = 0;
6+
let currSeq = 1;
7+
8+
for (let i = 1; i < sorted.length; i++) { // loop through sorted list to find sequence
9+
if (sorted[i - 1] + 1 === sorted[i]) {
10+
currSeq++;
11+
} else {
12+
longestSeq = Math.max(longestSeq, currSeq); // compare sequence to figure out the longest
13+
currSeq = 1;
14+
}
15+
}
16+
17+
return Math.max(longestSeq, currSeq);
18+
};
19+
20+
// time - O(nlong) using sort
21+
// space - O(n) store nums in set
22+
23+
24+
// TODO - try O(n) TC approach

‎maximum-product-subarray/kimyoung.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var maxProduct = function (nums) { // brute force approach - doesn't pass leetcode (Time Limit Exceeded)
2+
let maxProduct = -Infinity;
3+
for (let i = 0; i < nums.length; i++) { // get subarrays
4+
for (let j = i; j < nums.length; j++) {
5+
let prod = nums.slice(i, j + 1).reduce((acc, el) => acc *= el, 1);
6+
maxProduct = Math.max(prod, maxProduct);
7+
}
8+
}
9+
return maxProduct;
10+
};
11+
12+
// time - O(n^3) double for loop * reduce()
13+
// space - O(1)
14+
15+
var maxProduct = function (nums) { // DP approach
16+
let result = nums[0];
17+
let [min, max] = [1, 1];
18+
19+
for (const num of nums) {
20+
[min, max] = [Math.min(num * min, num * max, num), Math.max(num * min, num * max, num)];
21+
result = Math.max(max, result);
22+
}
23+
24+
return result;
25+
};
26+
27+
// time - O(n) looping through nums once
28+
// space - O(1) extra memory irrelevant to input

‎missing-number/kimyoung.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var missingNumber = function (nums) { // brute force approach
2+
let missingNumber;
3+
for (let i = 0; i < nums.length; i++) {
4+
if (nums.indexOf(i) === -1) missingNumber = i;
5+
}
6+
return missingNumber === undefined ? nums.length : missingNumber;
7+
};
8+
9+
// time - O(n^2) finding the index of i while iterating through nums
10+
// splace - O(1) no extra space needed other than missingNumber which just stores the result
11+
12+
var missingNumber = function (nums) { // set approach
13+
let set = new Set(nums);
14+
for (let i = 0; i < nums.length + 1; i++) {
15+
if (!set.has(i)) return i;
16+
}
17+
};
18+
19+
// time - O(n) looping through nums to find the missing number
20+
// splace - O(n) creating a set of nums
21+
22+
var missingNumber = function (nums) { // mathematical approach
23+
const len = nums.length
24+
const expectedSum = len * (len + 1) / 2;
25+
const actualSum = nums.reduce((acc, el) => acc += el, 0);
26+
return expectedSum - actualSum;
27+
};
28+
29+
// time - O(n) reduce method on actualSum
30+
// space - O(1) extra space irrelevant to input

‎valid-palindrome/kimyoung.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var isPalindrome = function (s) {
2+
let left = 0, right = s.length - 1;
3+
while (left < right) {
4+
while (left < right && !isAlphaNumeric(s[left])) { // increment the left index until it's an alphanumeric character
5+
left++;
6+
}
7+
while (left < right && !isAlphaNumeric(s[right])) { // decrement the right index until it's an alphanumeric character
8+
right--;
9+
}
10+
if (s[left++].toLowerCase() !== s[right--].toLowerCase()) return false; // compare the two string values, if different return false;
11+
}
12+
return true;
13+
};
14+
15+
function isAlphaNumeric(char) { // use ASCII code to findout if char is alphanumeric or not
16+
const asciiCode = char.charCodeAt(0);
17+
return (asciiCode >= 65 && asciiCode <= 90) ||
18+
(asciiCode >= 97 && asciiCode <= 122) ||
19+
(asciiCode >= 48 && asciiCode <= 57)
20+
}
21+
22+
// time - O(n) iterate through the string once
23+
// space - O(1) no extra space created

‎word-search/kimyoung.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Space Complexity O(m * n + w) approach
2+
var exist = function (board, word) {
3+
const rowLen = board.length, colLen = board[0].length;
4+
let visited = new Set(); // keep track of visited coordinates
5+
6+
function dfs(row, col, idx) {
7+
if (idx === word.length) return true; // if idx equals word.length, it means the word exists
8+
if (row < 0 || col < 0 ||
9+
row >= rowLen || col >= colLen ||
10+
board[row][col] !== word[idx] ||
11+
visited.has(`${row}|${col}`)) return false; // possible cases that would return false
12+
13+
14+
// backtracking
15+
visited.add(`${row}|${col}`);
16+
let result = dfs(row + 1, col, idx + 1) || // dfs on all 4 directions
17+
dfs(row - 1, col, idx + 1) ||
18+
dfs(row, col + 1, idx + 1) ||
19+
dfs(row, col - 1, idx + 1);
20+
visited.delete(`${row}|${col}`);
21+
22+
return result;
23+
}
24+
25+
for (let row = 0; row < rowLen; row++) {
26+
for (let col = 0; col < colLen; col++) {
27+
if(dfs(row, col, 0)) return true; // dfs for all coordinates
28+
}
29+
}
30+
31+
return false;
32+
};
33+
34+
// time - O(m * n * 4^w) traverse through the matrix (m * n) and run dfs on each of the possible paths (4^w) 4 being 4 directions
35+
// space - O(m * n + w)
36+
37+
// Space Complexity O(1) approach
38+
var exist = function (board, word) {
39+
const rowLen = board.length, colLen = board[0].length;
40+
41+
function dfs(row, col, idx) {
42+
if (idx === word.length) return true;
43+
if (row < 0 || col < 0 ||
44+
row >= rowLen || col >= colLen ||
45+
board[row][col] !== word[idx]) return false;
46+
47+
const letter = board[row][col];
48+
board[row][col] = '#'
49+
let result = dfs(row + 1, col, idx + 1) ||
50+
dfs(row - 1, col, idx + 1) ||
51+
dfs(row, col + 1, idx + 1) ||
52+
dfs(row, col - 1, idx + 1);
53+
board[row][col] = letter
54+
55+
return result;
56+
}
57+
58+
for (let row = 0; row < rowLen; row++) {
59+
for (let col = 0; col < colLen; col++) {
60+
if (board[row][col] === word[0] &&
61+
dfs(row, col, 0)) return true;
62+
}
63+
}
64+
65+
return false;
66+
};
67+
68+
// time - O(m * n * 4^w) traverse through the matrix (m * n) and run dfs on each of the possible paths (4^w) 4 being 4 directions
69+
// space - O(1)

0 commit comments

Comments
 (0)
Please sign in to comment.