From 4372706d77719cbcd899a82cc7a62aca791054a6 Mon Sep 17 00:00:00 2001 From: Young Kim Date: Sun, 1 Sep 2024 23:55:18 -0400 Subject: [PATCH 1/9] Valid Palindrome solution --- valid-palindrome/kimyoung.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 valid-palindrome/kimyoung.js diff --git a/valid-palindrome/kimyoung.js b/valid-palindrome/kimyoung.js new file mode 100644 index 000000000..67979aa7e --- /dev/null +++ b/valid-palindrome/kimyoung.js @@ -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) +} + +// time - O(n) iterate through the string once +// space - O(1) no extra space created From 9762c8ed19fb9632137d537c20ca7188bd12f6fe Mon Sep 17 00:00:00 2001 From: Young Kim Date: Mon, 2 Sep 2024 00:12:40 -0400 Subject: [PATCH 2/9] Missing Number solution --- missing-number/kimyoung.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 missing-number/kimyoung.js diff --git a/missing-number/kimyoung.js b/missing-number/kimyoung.js new file mode 100644 index 000000000..8a5162c49 --- /dev/null +++ b/missing-number/kimyoung.js @@ -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 \ No newline at end of file From 9fa51188c2cd3462479cea33268b30b5d38a6caa Mon Sep 17 00:00:00 2001 From: Young Kim Date: Mon, 2 Sep 2024 00:13:40 -0400 Subject: [PATCH 3/9] add end line break --- missing-number/kimyoung.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/missing-number/kimyoung.js b/missing-number/kimyoung.js index 8a5162c49..fdce44fa2 100644 --- a/missing-number/kimyoung.js +++ b/missing-number/kimyoung.js @@ -27,4 +27,4 @@ var missingNumber = function (nums) { // mathematical approach }; // time - O(n) reduce method on actualSum -// space - O(1) extra space irrelevant to input \ No newline at end of file +// space - O(1) extra space irrelevant to input From 43e64ab31656f8c635143473ee58e2de40691ede Mon Sep 17 00:00:00 2001 From: Young Kim Date: Mon, 2 Sep 2024 00:34:22 -0400 Subject: [PATCH 4/9] Longest Consecutive Sequence solution --- longest-consecutive-sequence/kimyoung.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 longest-consecutive-sequence/kimyoung.js diff --git a/longest-consecutive-sequence/kimyoung.js b/longest-consecutive-sequence/kimyoung.js new file mode 100644 index 000000000..408e75b09 --- /dev/null +++ b/longest-consecutive-sequence/kimyoung.js @@ -0,0 +1,24 @@ +var longestConsecutive = function (nums) { // sorting approach + if (!nums.length) return 0; + let set = new Set(nums); + 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 From eadc2e4c9f88c4615020bd7d44430cd1bacdecd0 Mon Sep 17 00:00:00 2001 From: Young Kim Date: Tue, 3 Sep 2024 12:20:19 -0400 Subject: [PATCH 5/9] Word Search solution --- word-search/kimyoung.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 word-search/kimyoung.js diff --git a/word-search/kimyoung.js b/word-search/kimyoung.js new file mode 100644 index 000000000..1be5efb8a --- /dev/null +++ b/word-search/kimyoung.js @@ -0,0 +1,34 @@ +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) From 8b3e2458bc1c7d17480f397c882ec8232031d3ee Mon Sep 17 00:00:00 2001 From: Young Kim Date: Tue, 3 Sep 2024 12:53:59 -0400 Subject: [PATCH 6/9] Space complexity optimization approach --- word-search/kimyoung.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/word-search/kimyoung.js b/word-search/kimyoung.js index 1be5efb8a..7ae818eda 100644 --- a/word-search/kimyoung.js +++ b/word-search/kimyoung.js @@ -1,3 +1,4 @@ +// 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 @@ -32,3 +33,37 @@ var exist = function (board, word) { // 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) From 0eb48f94150fda3f39f8977df99af958e121ae80 Mon Sep 17 00:00:00 2001 From: Young Kim Date: Tue, 3 Sep 2024 23:53:54 -0400 Subject: [PATCH 7/9] Maximum Product Subarray solution --- maximum-product-subarray/kimyoung.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 maximum-product-subarray/kimyoung.js diff --git a/maximum-product-subarray/kimyoung.js b/maximum-product-subarray/kimyoung.js new file mode 100644 index 000000000..d40fd9137 --- /dev/null +++ b/maximum-product-subarray/kimyoung.js @@ -0,0 +1,23 @@ +var maxProduct = function (nums) { // brute force approach + let subarrays = []; + for (let i = 0; i < nums.length; i++) { // get subarrays + for (let j = i; j < nums.length; j++) { + subarrays.push(nums.slice(i, j + 1)); + } + } + + let maxProduct = 0; + subarrays.forEach(arr => { // find product of each subarray and compare to maxProduct + let prod = arr.reduce((acc, el) => acc *= el, 1); + max = Math.max(prod, max); + }) + + return maxProduct; +}; + +// time - O(n^2) double for loop +// space - O(n^2) total count of subarrays = n*(n+1)/2 + + + +// TODO - different approach From 0f4e7cd98f35e29ae30521222d279c7dca5f6bf3 Mon Sep 17 00:00:00 2001 From: Young Kim Date: Tue, 3 Sep 2024 23:58:13 -0400 Subject: [PATCH 8/9] Optimized brute force approach --- maximum-product-subarray/kimyoung.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/maximum-product-subarray/kimyoung.js b/maximum-product-subarray/kimyoung.js index d40fd9137..ded7e5ff3 100644 --- a/maximum-product-subarray/kimyoung.js +++ b/maximum-product-subarray/kimyoung.js @@ -1,17 +1,11 @@ -var maxProduct = function (nums) { // brute force approach - let subarrays = []; +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++) { - subarrays.push(nums.slice(i, j + 1)); + let prod = nums.slice(i, j + 1).reduce((acc, el) => acc *= el, 1); + maxProduct = Math.max(prod, maxProduct); } } - - let maxProduct = 0; - subarrays.forEach(arr => { // find product of each subarray and compare to maxProduct - let prod = arr.reduce((acc, el) => acc *= el, 1); - max = Math.max(prod, max); - }) - return maxProduct; }; @@ -19,5 +13,4 @@ var maxProduct = function (nums) { // brute force approach // space - O(n^2) total count of subarrays = n*(n+1)/2 - // TODO - different approach From 7a2d50911534c4428dba3381504c541478a3bd71 Mon Sep 17 00:00:00 2001 From: Young Kim Date: Wed, 4 Sep 2024 23:30:52 -0400 Subject: [PATCH 9/9] Added DP approach --- maximum-product-subarray/kimyoung.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/maximum-product-subarray/kimyoung.js b/maximum-product-subarray/kimyoung.js index ded7e5ff3..9755ac5c6 100644 --- a/maximum-product-subarray/kimyoung.js +++ b/maximum-product-subarray/kimyoung.js @@ -9,8 +9,20 @@ var maxProduct = function (nums) { // brute force approach - doesn't pass leetco return maxProduct; }; -// time - O(n^2) double for loop -// space - O(n^2) total count of subarrays = n*(n+1)/2 +// 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]; -// TODO - different approach + 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