-
-
Notifications
You must be signed in to change notification settings - Fork 248
[Jeehay28] WEEK 15 Solutions #1658
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
4 commits
Select commit
Hold shift + click to select a range
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
44 changes: 44 additions & 0 deletions
44
construct-binary-tree-from-preorder-and-inorder-traversal/Jeehay28.ts
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,44 @@ | ||
class TreeNode { | ||
val: number; | ||
left: TreeNode | null; | ||
right: TreeNode | null; | ||
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
this.val = val === undefined ? 0 : val; | ||
this.left = left === undefined ? null : left; | ||
this.right = right === undefined ? null : right; | ||
} | ||
} | ||
|
||
// TC: O(n) | ||
// SC: O(n) | ||
function buildTree(preorder: number[], inorder: number[]): TreeNode | null { | ||
const indices = {}; | ||
// indices = { | ||
// 9: 0, | ||
// 3: 1, | ||
// 15: 2, | ||
// 20: 3, | ||
// 7: 4 | ||
// } | ||
|
||
for (let i = 0; i < inorder.length; i++) { | ||
const num = inorder[i]; | ||
indices[num] = i; | ||
} | ||
|
||
let preIndex = 0; | ||
|
||
const dfs = (start: number, end: number) => { | ||
if (start > end) return null; | ||
const val = preorder[preIndex]; | ||
preIndex++; | ||
const mid = indices[val]; | ||
|
||
const left = dfs(start, mid - 1); | ||
const right = dfs(mid + 1, end); | ||
|
||
return new TreeNode(val, left, right); | ||
}; | ||
|
||
return dfs(0, inorder.length - 1); | ||
} |
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 @@ | ||
// TC: O(n^2) | ||
// SC: O(1) | ||
function longestPalindrome(s: string): string { | ||
if (s.length < 2) return s; | ||
|
||
let maxLeft = 0; | ||
let maxRight = 0; | ||
|
||
const expandWindow = (left: number, right: number): void => { | ||
while (left >= 0 && right < s.length && s[left] === s[right]) { | ||
if (maxRight - maxLeft < right - left) { | ||
maxRight = right; | ||
maxLeft = left; | ||
} | ||
left--; | ||
right++; | ||
} | ||
}; | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
expandWindow(i, i); // odd length palindrome | ||
expandWindow(i, i + 1); // even length palindrome | ||
} | ||
|
||
return s.slice(maxLeft, maxRight + 1); | ||
} |
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 @@ | ||
/** | ||
Do not return anything, modify matrix in-place instead. | ||
*/ | ||
|
||
// TC: O(n^2) | ||
// SC: O(1) | ||
function rotate(matrix: number[][]): void { | ||
let top = 0; | ||
let bottom = matrix.length - 1; | ||
|
||
while (top < bottom) { | ||
let left = top; | ||
let right = bottom; | ||
|
||
for (let i = 0; i < bottom - top; i++) { | ||
const temp = matrix[top][left + i]; // topLeft | ||
matrix[top][left + i] = matrix[bottom - i][left]; | ||
matrix[bottom - i][left] = matrix[bottom][right - i]; | ||
matrix[bottom][right - i] = matrix[top + i][right]; | ||
matrix[top + i][right] = temp; | ||
} | ||
|
||
top++; // top down | ||
bottom--; // bottom up | ||
} | ||
} |
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 @@ | ||
class TreeNode { | ||
val: number; | ||
left: TreeNode | null; | ||
right: TreeNode | null; | ||
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
this.val = val === undefined ? 0 : val; | ||
this.left = left === undefined ? null : left; | ||
this.right = right === undefined ? null : right; | ||
} | ||
} | ||
|
||
// TC: O(m + n), m = number of nodes in root, n = number of nodes in subRoot | ||
// SC: O(m + n) | ||
function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean { | ||
const serializeNode = (node: TreeNode | null) => { | ||
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. 오 이런방법으로도 풀수있군요 👍🏻 성능도 더 좋게 나오겠네요 배워갑니다. |
||
if (!node) return "$"; | ||
|
||
const str = `(${node.val},${serializeNode(node.left)},${serializeNode( | ||
node.right | ||
)})`; | ||
|
||
return str; | ||
}; | ||
|
||
const serializedRoot = serializeNode(root); | ||
const serializedSubRoot = serializeNode(subRoot); | ||
|
||
return serializedRoot.includes(serializedSubRoot); | ||
} |
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.
오 저는 대각선 + 좌우반전으로 풀었는데 이렇게 풀수도있군요!