-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Sehwan] Week8 solutions with JavaScript #137
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
6 commits
Select commit
Hold shift + click to select a range
7aa73ea
Added combinationSum solution
nhistory 587c005
Added kthSamllestElement solution
nhistory 741a5b6
Added constructBinaryTree solution
nhistory df9552a
Added wordSearch solution
nhistory 96a0c39
Fixed time complexity for constructBinary
nhistory 8601acf
Added implementTrie solution
nhistory 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,26 @@ | ||
var combinationSum = function (candidates, target) { | ||
let output = []; | ||
|
||
// Helper function for depth-first search | ||
const dfs = (index, currentVal, arr) => { | ||
// If the remaining value is less than 0, no need to proceed further | ||
if (currentVal < 0) return; | ||
// If we have found a valid combination | ||
if (currentVal === 0) { | ||
output.push([...arr]); | ||
return; | ||
} | ||
|
||
// Iterate over the candidates starting from the current index | ||
for (let i = index; i < candidates.length; i++) { | ||
arr.push(candidates[i]); | ||
dfs(i, currentVal - candidates[i], arr); | ||
arr.pop(); // backtrack | ||
} | ||
}; | ||
|
||
// Start DFS with the initial target and empty combination | ||
dfs(0, target, []); | ||
|
||
return output; | ||
}; |
17 changes: 17 additions & 0 deletions
17
construct-binary-tree-from-preorder-and-inorder-traversal/nhistory.js
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,17 @@ | ||
var buildTree = function (preorder, inorder) { | ||
// Edge case: if either preorder or inorder is empty, return null | ||
if (!preorder.length || !inorder.length) return null; | ||
|
||
// The first element in preorder is the root of the tree | ||
// Find the index of the root in the inorder array | ||
const root = new TreeNode(preorder[0]); | ||
const mid = inorder.indexOf(root.val); | ||
|
||
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid)); | ||
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1)); | ||
|
||
return root; | ||
}; | ||
|
||
// TC: O(n^2) | ||
// SC: O(n) |
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,51 @@ | ||
var Trie = function () { | ||
this.root = {}; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
Trie.prototype.insert = function (word) { | ||
let node = this.root; | ||
for (let char of word) { | ||
if (!node[char]) { | ||
node[char] = {}; | ||
} | ||
node = node[char]; | ||
} | ||
node.isEndOfWord = true; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.search = function (word) { | ||
let node = this.root; | ||
for (let char of word) { | ||
if (!node[char]) { | ||
return false; | ||
} | ||
node = node[char]; | ||
} | ||
return node.isEndOfWord === true; | ||
}; | ||
|
||
/** | ||
* @param {string} prefix | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.startsWith = function (prefix) { | ||
let node = this.root; | ||
for (let char of prefix) { | ||
if (!node[char]) { | ||
return false; | ||
} | ||
node = node[char]; | ||
} | ||
return true; | ||
}; | ||
|
||
// TC: O(n) | ||
// SC: O(n) |
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,19 @@ | ||
var kthSmallest = function (root, k) { | ||
let arr = []; | ||
inOrder(root, arr); | ||
|
||
for (let i = 0; i < arr.length; i++) { | ||
if (i === k - 1) return arr[i]; | ||
} | ||
}; | ||
|
||
const inOrder = (root, arr) => { | ||
if (!root) return; | ||
|
||
inOrder(root.left, arr); | ||
arr.push(root.val); | ||
inOrder(root.right, arr); | ||
}; | ||
|
||
// TC: O(n) | ||
// SC: O(n) |
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,36 @@ | ||
var exist = function (board, word) { | ||
const dfs = (row, col, index) => { | ||
if (word.length === index) return true; | ||
if ( | ||
row < 0 || | ||
col < 0 || | ||
row >= board.length || | ||
col >= board[0].length || | ||
board[row][col] !== word[index] | ||
) | ||
return false; | ||
|
||
board[row][col] = "#"; | ||
if ( | ||
dfs(row + 1, col, index + 1) || | ||
dfs(row, col + 1, index + 1) || | ||
dfs(row - 1, col, index + 1) || | ||
dfs(row, col - 1, index + 1) | ||
) | ||
return true; | ||
|
||
board[row][col] = word[index]; | ||
}; | ||
|
||
for (let row = 0; row < board.length; row++) { | ||
for (let col = 0; col < board[row].length; col++) { | ||
if (board[row][col] === word[0] && dfs(row, col, 0)) return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
// N: board column length / M: board row length / L: word length | ||
// TC: O(N*M*4^L) | ||
// SC: O(L) |
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.