-
-
Notifications
You must be signed in to change notification settings - Fork 195
[kimyoung] WEEK 04 Solutions #413
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4372706
Valid Palindrome solution
kim-young 9762c8e
Missing Number solution
kim-young 9fa5118
add end line break
kim-young 43e64ab
Longest Consecutive Sequence solution
kim-young eadc2e4
Word Search solution
kim-young 8b3e245
Space complexity optimization approach
kim-young 0eb48f9
Maximum Product Subarray solution
kim-young 0f4e7cd
Optimized brute force approach
kim-young 7a2d509
Added DP approach
kim-young File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여러가지 패턴으로 푸는거 좋네요 👍 |
This file contains hidden or 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 hidden or 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) | ||
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 |
This file contains hidden or 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.