Skip to content
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

[kimyoung] WEEK 04 Solutions #413

Merged
merged 9 commits into from
Sep 6, 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
24 changes: 24 additions & 0 deletions longest-consecutive-sequence/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var longestConsecutive = function (nums) { // sorting approach
if (!nums.length) return 0;
let set = new Set(nums);
kim-young marked this conversation as resolved.
Show resolved Hide resolved
let sorted = [...set].sort((a, b) => a - b);
let longestSeq = 0;
let currSeq = 1;

for (let i = 1; i < sorted.length; i++) { // loop through sorted list to find sequence
if (sorted[i - 1] + 1 === sorted[i]) {
currSeq++;
} else {
longestSeq = Math.max(longestSeq, currSeq); // compare sequence to figure out the longest
currSeq = 1;
}
}

return Math.max(longestSeq, currSeq);
};

// time - O(nlong) using sort
// space - O(n) store nums in set


// TODO - try O(n) TC approach
28 changes: 28 additions & 0 deletions maximum-product-subarray/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var maxProduct = function (nums) { // brute force approach - doesn't pass leetcode (Time Limit Exceeded)
let maxProduct = -Infinity;
for (let i = 0; i < nums.length; i++) { // get subarrays
for (let j = i; j < nums.length; j++) {
let prod = nums.slice(i, j + 1).reduce((acc, el) => acc *= el, 1);
maxProduct = Math.max(prod, maxProduct);
}
}
return maxProduct;
};

// time - O(n^3) double for loop * reduce()
// space - O(1)

var maxProduct = function (nums) { // DP approach
let result = nums[0];
let [min, max] = [1, 1];

for (const num of nums) {
[min, max] = [Math.min(num * min, num * max, num), Math.max(num * min, num * max, num)];
result = Math.max(max, result);
}

return result;
};

// time - O(n) looping through nums once
// space - O(1) extra memory irrelevant to input
30 changes: 30 additions & 0 deletions missing-number/kimyoung.js
Copy link
Contributor

Choose a reason for hiding this comment

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

여러가지 패턴으로 푸는거 좋네요 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var missingNumber = function (nums) { // brute force approach
let missingNumber;
for (let i = 0; i < nums.length; i++) {
if (nums.indexOf(i) === -1) missingNumber = i;
}
return missingNumber === undefined ? nums.length : missingNumber;
};

// time - O(n^2) finding the index of i while iterating through nums
// splace - O(1) no extra space needed other than missingNumber which just stores the result

var missingNumber = function (nums) { // set approach
let set = new Set(nums);
for (let i = 0; i < nums.length + 1; i++) {
if (!set.has(i)) return i;
}
};

// time - O(n) looping through nums to find the missing number
// splace - O(n) creating a set of nums

var missingNumber = function (nums) { // mathematical approach
const len = nums.length
const expectedSum = len * (len + 1) / 2;
const actualSum = nums.reduce((acc, el) => acc += el, 0);
return expectedSum - actualSum;
};

// time - O(n) reduce method on actualSum
// space - O(1) extra space irrelevant to input
23 changes: 23 additions & 0 deletions valid-palindrome/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var isPalindrome = function (s) {
let left = 0, right = s.length - 1;
while (left < right) {
while (left < right && !isAlphaNumeric(s[left])) { // increment the left index until it's an alphanumeric character
left++;
}
while (left < right && !isAlphaNumeric(s[right])) { // decrement the right index until it's an alphanumeric character
right--;
}
if (s[left++].toLowerCase() !== s[right--].toLowerCase()) return false; // compare the two string values, if different return false;
}
return true;
};

function isAlphaNumeric(char) { // use ASCII code to findout if char is alphanumeric or not
const asciiCode = char.charCodeAt(0);
return (asciiCode >= 65 && asciiCode <= 90) ||
(asciiCode >= 97 && asciiCode <= 122) ||
(asciiCode >= 48 && asciiCode <= 57)
DaleSeo marked this conversation as resolved.
Show resolved Hide resolved
}

// time - O(n) iterate through the string once
// space - O(1) no extra space created
69 changes: 69 additions & 0 deletions word-search/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Space Complexity O(m * n + w) approach
var exist = function (board, word) {
const rowLen = board.length, colLen = board[0].length;
let visited = new Set(); // keep track of visited coordinates

function dfs(row, col, idx) {
if (idx === word.length) return true; // if idx equals word.length, it means the word exists
if (row < 0 || col < 0 ||
row >= rowLen || col >= colLen ||
board[row][col] !== word[idx] ||
visited.has(`${row}|${col}`)) return false; // possible cases that would return false


// backtracking
visited.add(`${row}|${col}`);
let result = dfs(row + 1, col, idx + 1) || // dfs on all 4 directions
dfs(row - 1, col, idx + 1) ||
dfs(row, col + 1, idx + 1) ||
dfs(row, col - 1, idx + 1);
visited.delete(`${row}|${col}`);

return result;
}

for (let row = 0; row < rowLen; row++) {
for (let col = 0; col < colLen; col++) {
if(dfs(row, col, 0)) return true; // dfs for all coordinates
}
}

return false;
};

// 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
// space - O(m * n + w)

// Space Complexity O(1) approach
var exist = function (board, word) {
const rowLen = board.length, colLen = board[0].length;

function dfs(row, col, idx) {
if (idx === word.length) return true;
if (row < 0 || col < 0 ||
row >= rowLen || col >= colLen ||
board[row][col] !== word[idx]) return false;

const letter = board[row][col];
board[row][col] = '#'
let result = dfs(row + 1, col, idx + 1) ||
dfs(row - 1, col, idx + 1) ||
dfs(row, col + 1, idx + 1) ||
dfs(row, col - 1, idx + 1);
board[row][col] = letter

return result;
}

for (let row = 0; row < rowLen; row++) {
for (let col = 0; col < colLen; col++) {
if (board[row][col] === word[0] &&
dfs(row, col, 0)) return true;
}
}

return false;
};

// 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
// space - O(1)