From b6f3e9c4869f1745f7f97a3b95ac32c6c1bfd24e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 23 Nov 2024 06:44:02 +0900 Subject: [PATCH 1/3] Add week 15 solutions: subtree-of-another-tree --- subtree-of-another-tree/gitsunmin.ts | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 subtree-of-another-tree/gitsunmin.ts diff --git a/subtree-of-another-tree/gitsunmin.ts b/subtree-of-another-tree/gitsunmin.ts new file mode 100644 index 000000000..21e156a2c --- /dev/null +++ b/subtree-of-another-tree/gitsunmin.ts @@ -0,0 +1,29 @@ +/** + * https://leetcode.com/problems/subtree-of-another-tree/ + * time complexity : O(n * m) + * space complexity : O(n) + */ + +class TreeNode { + val: number; + left: TreeNode | null; + right: TreeNode | null; + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + this.val = val ?? 0; + this.left = left ?? null; + this.right = right ?? null; + } +} + +function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + if (!p && !q) return true; + if (!p || !q || p.val !== q.val) return false; + + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); +} + +function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean { + if (!root) return false; + if (isSameTree(root, subRoot)) return true; + return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot); +} From a76bdc4b45c982738e6f940e81b13f2eaebb539f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 23 Nov 2024 06:53:16 +0900 Subject: [PATCH 2/3] Add week 15 solutions: validate-binary-search-tree --- validate-binary-search-tree/gitsunmin.ts | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 validate-binary-search-tree/gitsunmin.ts diff --git a/validate-binary-search-tree/gitsunmin.ts b/validate-binary-search-tree/gitsunmin.ts new file mode 100644 index 000000000..9d0c2c9e2 --- /dev/null +++ b/validate-binary-search-tree/gitsunmin.ts @@ -0,0 +1,32 @@ +/** + * https://leetcode.com/problems/validate-binary-search-tree/ + * time complexity : O(n) + * space complexity : O(n) + */ + +class TreeNode { + val: number; + left: TreeNode | null; + right: TreeNode | null; + constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + this.val = val ?? 0; + this.left = left ?? null; + this.right = right ?? null; + } +} + +function validate(node: TreeNode | null, lower: number, upper: number): boolean { + if (!node) return true; + const val = node.val; + + if (val <= lower || val >= upper) return false; + if (!validate(node.right, val, upper)) return false; + if (!validate(node.left, lower, val)) return false; + + return true; +} + +function isValidBST(root: TreeNode | null): boolean { + + return validate(root, -Infinity, Infinity); +} From a9d406d8fea1a4885b4efbae17856992a759befe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 23 Nov 2024 07:00:18 +0900 Subject: [PATCH 3/3] Add week 15 solutions: longest-palindromic-substring/ --- longest-palindromic-substring/gitsunmin.ts | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 longest-palindromic-substring/gitsunmin.ts diff --git a/longest-palindromic-substring/gitsunmin.ts b/longest-palindromic-substring/gitsunmin.ts new file mode 100644 index 000000000..535c01c1a --- /dev/null +++ b/longest-palindromic-substring/gitsunmin.ts @@ -0,0 +1,45 @@ +/** + * https://leetcode.com/problems/longest-palindromic-substring/ + * time complexity : O(n) + * space complexity : O(n^2) + */ + +function findPalindromeAroundCenter(s: string, leftIndex: number, rightIndex: number): [number, number] { + while (leftIndex >= 0 && rightIndex < s.length && s[leftIndex] === s[rightIndex]) { + leftIndex--; // 왼쪽으로 확장 + rightIndex++; // 오른쪽으로 확장 + } + + // 팰린드롬의 시작과 끝 인덱스 반환 + return [leftIndex + 1, rightIndex - 1]; +} + +function longestPalindrome(s: string): string { + if (s.length <= 1) return s; + + let longestPalindromeStartIndex = 0; + let longestPalindromeLength = 0; + + for (let centerIndex = 0; centerIndex < s.length; centerIndex++) { + // 홀수 길이 팰린드롬 확인 + const [oddStart, oddEnd] = findPalindromeAroundCenter(s, centerIndex, centerIndex); + const oddLength = oddEnd - oddStart + 1; + + if (oddLength > longestPalindromeLength) { + longestPalindromeStartIndex = oddStart; + longestPalindromeLength = oddLength; + } + + // 짝수 길이 팰린드롬 확인 + const [evenStart, evenEnd] = findPalindromeAroundCenter(s, centerIndex, centerIndex + 1); + const evenLength = evenEnd - evenStart + 1; + + if (evenLength > longestPalindromeLength) { + longestPalindromeStartIndex = evenStart; + longestPalindromeLength = evenLength; + } + } + + // 가장 긴 팰린드롬 부분 문자열 반환 + return s.substring(longestPalindromeStartIndex, longestPalindromeStartIndex + longestPalindromeLength); +}