Skip to content

[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 4 commits into from
Jul 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
26 changes: 26 additions & 0 deletions longest-palindromic-substring/Jeehay28.ts
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);
}
26 changes: 26 additions & 0 deletions rotate-image/Jeehay28.ts
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 저는 대각선 + 좌우반전으로 풀었는데 이렇게 풀수도있군요!

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
}
}
29 changes: 29 additions & 0 deletions subtree-of-another-tree/Jeehay28.ts
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}