-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #413 from kim-young/main
[kimyoung] WEEK 04 Solutions
- Loading branch information
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
// time - O(n) iterate through the string once | ||
// space - O(1) no extra space created |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |