Skip to content

[Lyla] Week 15 #1114

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 5 commits into from
Mar 22, 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions longest-palindromic-substring/lylaminju.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'''
시간 복잡도: O(n^2)
공간 복잡도: O(1)
'''

class Solution:
def longestPalindrome(self, s: str) -> str:
start, max_length = 0, 1 # Track longest palindrome

def expand_around_center(left: int, right: int) -> int:
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
# Return length of palindrome
return right - left - 1

# Check each position as potential center
for i in range(len(s)):
# Check for odd length palindromes (single character center)
len1 = expand_around_center(i, i)
# Check for even length palindromes (between two characters)
len2 = expand_around_center(i, i + 1)

curr_max = max(len1, len2)

# Update start and max_length if current palindrome is longer
if curr_max > max_length:
max_length = curr_max
start = i - (curr_max - 1) // 2

return s[start:start + max_length]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions rotate-image/lylaminju.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
시간 복잡도: O(n^2)
공간 복잡도: O(1)
'''
from typing import List


class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
n = len(matrix)

for i in range(n // 2):
k = (n - 1) - i * 2

for j in range(k):
end = i + k

matrix[i][i + j], matrix[i + j][end], matrix[end][end - j], matrix[end - j][i] = matrix[end - j][i], matrix[i][i + j], matrix[i + j][end], matrix[end][end - j]


class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
n = len(matrix)

# Step 1: Transpose matrix (swap elements across diagonal)
for i in range(n):
for j in range(i, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

# Step 2: Reverse each row
for i in range(n):
matrix[i].reverse()
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions subtree-of-another-tree/lylaminju.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'''
시간 복잡도: O(m * n)
- m은 root 트리의 노드 수, n은 subRoot 트리의 노드 수

공간 복잡도: O(h)
- h는 root 트리의 높이
'''
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
from typing import Optional


class Solution:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
if not subRoot:
return True

if not root:
return False

return self.isSameTree(root, subRoot) or self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)

def isSameTree(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
if not root1 and not root2:
return True

if not root1 or not root2:
return False

return root1.val == root2.val and self.isSameTree(root1.left, root2.left) and self.isSameTree(root1.right, root2.right)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions validate-binary-search-tree/lylaminju.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'''
시간 복잡도: O(n)
- n은 트리의 노드 수

공간 복잡도: O(h)
- h는 트리의 높이
'''
from typing import Optional

# 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

# 현재 노드 값이 허용 범위를 벗어나면 False
if node.val <= min_val or 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'))
File renamed without changes.
File renamed without changes.
File renamed without changes.