-
Notifications
You must be signed in to change notification settings - Fork 126
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
[gitsunmin] Week15 Solutions #610
Changes from 3 commits
b6f3e9c
a76bdc4
a9d406d
9ee119f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
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. 중요한 사항은 아닙니다! |
||
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
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. 재귀함수의 early return에서 조심할 점이 있다면 단축평가에서 손해보는 점인 것 같습니다. 그리고 if-else가 아니라 if문 단독으로 쓰는 경우, 개인적으로 각 if 문들의 조건이 무조건 독립적이도록 짜면 어떨까 싶습니다. |
||
|
||
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
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. 👍 |
||
} |
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.
시간, 공간 복잡도를 이렇게 판단하신 이유가 있으실까요?
제 생각에 시간 복잡도는 최대
N * N/2
로O(n^2)
이고 공간복잡도는 리턴값을 고려하더라도O(n)
, 제외한다면O(1)
로 볼 수 있을 것 같아서요!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.
이렇게 계산했어요!
• for 루프: 문자열 길이 n에 따라 n번 반복 → O(n).
• 팰린드롬 확장: 각 중심에서 최대 문자열 길이만큼 확장 → O(n).
• 전체 시간 복잡도: O(n) × O(n) = O(n²).
최악의 경우, 모든 중심에서 최대 확장되므로 O(n²)