-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Jeehay28] WEEK 06 #891
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
[Jeehay28] WEEK 06 #891
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7287a87
Add container-with-most-water solution
Jeehay28 b2a6a85
Add valid-parentheses solution
Jeehay28 50b3ab9
Add design-add-and-search-words-data-structure solution
Jeehay28 b64d18b
Add spiral-matrix solution
Jeehay28 ec041f9
Add longest-increasing-subsequence solution
Jeehay28 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,29 @@ | ||
/** | ||
* @param {number[]} height | ||
* @return {number} | ||
*/ | ||
|
||
// Time Complexity: O(n) | ||
// Space Complexity: O(1) | ||
var maxArea = function (height) { | ||
let start = 0; | ||
let end = height.length - 1; | ||
let maxArea = 0; | ||
|
||
while (start < end) { | ||
const area = (end - start) * Math.min(height[start], height[end]); | ||
maxArea = Math.max(area, maxArea); | ||
|
||
// The shorter height limits the area. | ||
// By moving the pointer associated with the shorter height, | ||
// the algorithm maximizes the chance of finding a taller line that can increase the area. | ||
// This is the essence of the two-pointer strategy for the container problem. | ||
if (height[start] < height[end]) { | ||
start += 1; | ||
} else { | ||
end -= 1; | ||
} | ||
} | ||
return maxArea; | ||
}; | ||
|
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,187 @@ | ||
/** | ||
* ==================== Complexity Summary ==================== | ||
* | Operation | Time Complexity | Space Complexity | | ||
* |------------|------------------------|--------------------| | ||
* | addWord | O(n) | O(n) | | ||
* | search | O(n) (no '.') | O(n) | | ||
* | | O(k^n) (with '.') | O(n) | | ||
* ============================================================ | ||
* | ||
* Notes: | ||
* - n: Length of the word being added/searched. | ||
* - k: Average branching factor (number of children per node). | ||
* - Worst-case time complexity for `search` with '.' can grow exponentially | ||
* when many wildcards are present in the word. | ||
*/ | ||
|
||
var WordDictionary = function () { | ||
this.root = { $: false }; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
WordDictionary.prototype.addWord = function (word) { | ||
let node = this.root; | ||
|
||
for (char of word) { | ||
if (!node[char]) { | ||
node[char] = { $: false }; | ||
} | ||
node = node[char]; | ||
} | ||
|
||
node["$"] = true; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
WordDictionary.prototype.search = function (word) { | ||
const dfs = (node, index) => { | ||
// Base case: If we've reached the end of the word | ||
if (index === word.length) { | ||
return node["$"] === true; | ||
} | ||
|
||
const char = word[index]; | ||
|
||
// If the character is not a wildcard | ||
if (char !== ".") { | ||
if (node[char]) { | ||
return dfs(node[char], index + 1); // Continue DFS | ||
} else { | ||
return false; // Character not found | ||
} | ||
} | ||
|
||
// If the character is a wildcard | ||
for (const key of Object.keys(node).filter((key) => key !== "$")) { | ||
if (dfs(node[key], index + 1)) { | ||
return true; // Return true if any child path matches | ||
} | ||
// Why we need to check if the recursive call to dfs returns true or false: | ||
// 1) Early termination of recursion when a match is found: | ||
// - Once we find a valid match, there's no need to explore other branches. | ||
// 2) Proper propagation of the result back up the recursion stack: | ||
// - A valid match found in a deeper level of recursion needs to return true | ||
// all the way back to the initial search call to indicate success. | ||
} | ||
|
||
return false; // No paths matched for the wildcard | ||
}; | ||
|
||
return dfs(this.root, 0); // Start DFS from the root node | ||
}; | ||
|
||
/** | ||
* Your WordDictionary object will be instantiated and called as such: | ||
* var obj = new WordDictionary() | ||
* obj.addWord(word) | ||
* var param_2 = obj.search(word) | ||
*/ | ||
|
||
// ***** second trial ***** | ||
// var WordDictionary = function () { | ||
// this.root = { $: true }; | ||
// }; | ||
|
||
// /** | ||
// * @param {string} word | ||
// * @return {void} | ||
// */ | ||
// WordDictionary.prototype.addWord = function (word) { | ||
// const dfs = (node, index) => { | ||
// // exit | ||
// if (index === word.length) { | ||
// node["$"] = true; | ||
// return; | ||
// } | ||
|
||
// // add . | ||
// if (!node["."]) { | ||
// node["."] = { $: false }; | ||
// } | ||
// dfs(node["."], index + 1); | ||
|
||
// // add each character | ||
// const temp = word[index]; | ||
|
||
// if (!node[temp]) { | ||
// node[temp] = { $: false }; | ||
// } | ||
// dfs(node[temp], index + 1); | ||
// }; | ||
|
||
// dfs(this.root, 0); | ||
// }; | ||
|
||
// /** | ||
// * @param {string} word | ||
// * @return {boolean} | ||
// */ | ||
// WordDictionary.prototype.search = function (word) { | ||
// let node = this.root; | ||
|
||
// for (char of word) { | ||
// if (!node[char]) { | ||
// return false; | ||
// } | ||
// node = node[char]; | ||
// } | ||
|
||
// return node["$"]; | ||
// }; | ||
|
||
/** | ||
* Your WordDictionary object will be instantiated and called as such: | ||
* var obj = new WordDictionary() | ||
* obj.addWord(word) | ||
* var param_2 = obj.search(word) | ||
*/ | ||
|
||
// ***** first trial ***** | ||
// var WordDictionary = function () { | ||
// this.root = new Set(); | ||
// }; | ||
|
||
// /** | ||
// * @param {string} word | ||
// * @return {void} | ||
// */ | ||
// WordDictionary.prototype.addWord = function (word) { | ||
// this.root.add(word); | ||
// }; | ||
|
||
// /** | ||
// * @param {string} word | ||
// * @return {boolean} | ||
// */ | ||
// WordDictionary.prototype.search = function (word) { | ||
// if (this.root.size === 0) { | ||
// return false; | ||
// } else { | ||
// for (dict of this.root) { | ||
// if (dict.length === word.length) { | ||
// const found = word | ||
// .split("") | ||
// .every((str, index) => str === "." || dict[index] === str); | ||
// if (found) { | ||
// return true; | ||
// } | ||
// } else { | ||
// continue; | ||
// } | ||
// } | ||
// return false; | ||
// } | ||
// }; | ||
|
||
/** | ||
* Your WordDictionary object will be instantiated and called as such: | ||
* var obj = new WordDictionary() | ||
* obj.addWord(word) | ||
* var param_2 = obj.search(word) | ||
*/ |
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,26 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
|
||
// Time Complexity: O(n^) | ||
// Space Complexity: O(n) | ||
var lengthOfLIS = function (nums) { | ||
if (nums.length === 0) { | ||
return 0; | ||
} | ||
|
||
let dp = new Array(nums.length).fill(1); // dp[i] will be the length of LIS ending at i | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
for (let j = 0; j < i; j++) { | ||
if (nums[i] > nums[j]) { | ||
// Strictly increasing condition | ||
dp[i] = Math.max(dp[i], dp[j] + 1); | ||
} | ||
} | ||
} | ||
|
||
return Math.max(...dp); // The length of the longest subsequence | ||
}; | ||
|
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,66 @@ | ||
// Time Complexity: O(m * n), where m is the number of rows and n is the number of columns in the matrix, | ||
// because every element is processed once. | ||
|
||
// Space Complexity: O(m * n), where m is the number of rows and n is the number of columns in the matrix, | ||
// because we store all matrix elements in the result array. | ||
|
||
/** | ||
* @param {number[][]} matrix | ||
* @return {number[]} | ||
*/ | ||
var spiralOrder = function (matrix) { | ||
|
||
let topRow = 0; | ||
let bottomRow = matrix.length - 1; | ||
let leftCol = 0; | ||
let rightCol = matrix[0].length - 1; | ||
let result = []; | ||
|
||
while (topRow <= bottomRow && leftCol <= rightCol) { | ||
// move to the right | ||
for (let col = leftCol; col <= rightCol; col++) { | ||
result.push(matrix[topRow][col]); | ||
} | ||
|
||
topRow += 1; | ||
|
||
if (topRow > bottomRow) { | ||
break; | ||
} | ||
|
||
// move down | ||
for (let row = topRow; row <= bottomRow; row++) { | ||
result.push(matrix[row][rightCol]); | ||
} | ||
|
||
rightCol -= 1; | ||
|
||
if (leftCol > rightCol) { | ||
break; | ||
} | ||
|
||
// move to the left | ||
for (let col = rightCol; col >= leftCol; col--) { | ||
result.push(matrix[bottomRow][col]); | ||
} | ||
|
||
bottomRow -= 1; | ||
if (topRow > bottomRow) { | ||
break; | ||
} | ||
|
||
// move up | ||
for (let row = bottomRow; row >= topRow; row--) { | ||
result.push(matrix[row][leftCol]); | ||
} | ||
|
||
leftCol += 1; | ||
|
||
if (leftCol > rightCol) { | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
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,35 @@ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
|
||
// Time Complexity: O(n) | ||
// Space Complexity: O(n) | ||
var isValid = function (s) { | ||
const obj = { | ||
"(" : ")", | ||
"{" : "}", | ||
"[" : "]", | ||
}; | ||
|
||
let stack = []; | ||
|
||
for (any of s) { | ||
// open bracket | ||
if (obj[any]) { | ||
stack.push(any); | ||
} else { | ||
// close bracket | ||
if (stack.length === 0) { | ||
return false; | ||
} else if (obj[stack[stack.length - 1]] !== any) { | ||
return false; | ||
} else { | ||
stack.pop(); | ||
} | ||
} | ||
} | ||
return stack.length === 0 ? true : false; | ||
}; | ||
|
||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return stack.length === 0
만 해도 true, false 로 반환되지 않을까요? :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아, 그렇네요. 감사합니다.