diff --git a/longest-palindromic-substring/KwonNayeon.py b/longest-palindromic-substring/KwonNayeon.py new file mode 100644 index 000000000..81a367645 --- /dev/null +++ b/longest-palindromic-substring/KwonNayeon.py @@ -0,0 +1,31 @@ +""" +Constraints: +- 1 <= s.length <= 1000 +- s consist of only digits and English letters. + +Time Complexity: O(n^3) +- 모든 부분 문자열을 구할 때 O(n^2) +- 각 부분 문자열이 팰린드롬인지를 알아낼 때 O(n) + +Space Complexity: O(1) + +Note: +- 더 효율적인 방법 생각해보기/찾아보기 +""" +# Solution 1: Brute force +# 문자열의 시작값과 끝값을 이용하여 가장 긴 팰린드롬으로 업데이트하는 방식 +class Solution: + def longestPalindrome(self, s: str) -> str: + longest_palindrome = "" + max_len = 0 + + for i in range(len(s)): + for j in range(i, len(s)): + substr = s[i:j+1] + + if substr == substr[::-1]: + if len(substr) > max_len: + max_len = len(substr) + longest_palindrome = substr + + return longest_palindrome diff --git a/rotate-image/KwonNayeon.py b/rotate-image/KwonNayeon.py new file mode 100644 index 000000000..30595d510 --- /dev/null +++ b/rotate-image/KwonNayeon.py @@ -0,0 +1,15 @@ +""" +Constraints: +- n == matrix.length == matrix[i].length +- 1 <= n <= 20 +- -1000 <= matrix[i][j] <= 1000 + +Time Complexity: +- + +Space Complexity: +- + +풀이방법: +1. +""" diff --git a/subtree-of-another-tree/KwonNayeon.py b/subtree-of-another-tree/KwonNayeon.py new file mode 100644 index 000000000..ac4acb616 --- /dev/null +++ b/subtree-of-another-tree/KwonNayeon.py @@ -0,0 +1,56 @@ +""" +Constraints: +- The number of nodes in the root tree is in the range [1, 2000]. +- The number of nodes in the subRoot tree is in the range [1, 1000]. +- -10^4 <= root.val <= 10^4 +- -10^4 <= subRoot.val <= 10^4 + +Time Complexity: O(m * n) +- m: root의 노드 수 +- n: subRoot의 노드 수 + +Space Complexity: O(m) +- 재귀 호출 스택의 최대 깊이는 root의 높이 + +풀이방법: +1. Base case: + - subRoot가 없는 경우 True + - root가 빈 트리이고, subRoot가 있는 경우 False +2. 서브트리가 동일한지를 확인하는 재귀 함수 활용: + - isSameTree()를 사용하여 두 트리가 동일한지 판단 + - 현재 노드부터 시작해 subRoot와 같은지 확인 + - 같지 않다면 왼쪽과 오른쪽 서브트리를 다시 검사함 +3. 재귀적으로 서브트리를 탐색 + - 현재 노드에서 시작하는 서브트리가 subRoot와 같다면 True + - 아니라면 왼쪽 또는 오른쪽 서브트리에서 계속 탐색 +""" +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: + if not subRoot: + return True + + if not root: + return False + + if self.isSameTree(root, subRoot): + return True + + return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot) + + def isSameTree(self, p: TreeNode, q: TreeNode): + if not p and not q: + return True + + if not p or not q: + return False + + if p.val != q.val: + return False + + return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) diff --git a/validate-binary-search-tree/KwonNayeon.py b/validate-binary-search-tree/KwonNayeon.py new file mode 100644 index 000000000..5535a5745 --- /dev/null +++ b/validate-binary-search-tree/KwonNayeon.py @@ -0,0 +1,42 @@ +""" +Constraints: +- The number of nodes in the tree is in the range [1, 10^4]. +- -2^31 <= Node.val <= 2^31 - 1 + +Time Complexity: O(n) +- 트리의 모든 노드를 한 번씩 방문함 + +Space Complexity: O(h) +- 재귀 호출 스택의 최대 깊이는 트리의 높이 + +풀이방법: +1. 각 노드가 가질 수 있는 값의 범위를 한정함 + - root 노드의 범위는 (-무한대, +무한대)로 설정 +2. Base case: + - 빈 노드의 경우 True + - 노드 값이 범위를 벗어나면 False +3. 재귀를 활용: + - 왼쪽 서브트리: max_val를 현재 노드 값으로 업데이트 (모두 현재 값보다 작아야 함) + - 오른쪽 서브트리: min_val을 현재 노드 값으로 업데이트 (모두 현재 값보다 커야 함) + - 왼쪽과 오른쪽 서브트리가 모두 조건을 만족하면 BST +""" +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isValidBST(self, root: Optional[TreeNode]) -> bool: + def validate(node, min_val, max_val): + if not node: + return True + + if not (min_val < node.val < max_val): + return False + + return (validate(node.left, min_val, node.val) and + validate(node.right, node.val, max_val)) + + return validate(root, float("-inf"), float("inf")) +