Skip to content

Commit 07c09ac

Browse files
authored
Merge pull request #1114 from lylaminju/main
[Lyla] Week 15
2 parents 433ca7a + 90fda94 commit 07c09ac

File tree

69 files changed

+127
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+127
-0
lines changed
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n^2)
3+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
4+
'''
5+
6+
class Solution:
7+
def longestPalindrome(self, s: str) -> str:
8+
start, max_length = 0, 1 # Track longest palindrome
9+
10+
def expand_around_center(left: int, right: int) -> int:
11+
while left >= 0 and right < len(s) and s[left] == s[right]:
12+
left -= 1
13+
right += 1
14+
# Return length of palindrome
15+
return right - left - 1
16+
17+
# Check each position as potential center
18+
for i in range(len(s)):
19+
# Check for odd length palindromes (single character center)
20+
len1 = expand_around_center(i, i)
21+
# Check for even length palindromes (between two characters)
22+
len2 = expand_around_center(i, i + 1)
23+
24+
curr_max = max(len1, len2)
25+
26+
# Update start and max_length if current palindrome is longer
27+
if curr_max > max_length:
28+
max_length = curr_max
29+
start = i - (curr_max - 1) // 2
30+
31+
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.

โ€Žrotate-image/lylaminju.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n^2)
3+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
4+
'''
5+
from typing import List
6+
7+
8+
class Solution:
9+
def rotate(self, matrix: List[List[int]]) -> None:
10+
n = len(matrix)
11+
12+
for i in range(n // 2):
13+
k = (n - 1) - i * 2
14+
15+
for j in range(k):
16+
end = i + k
17+
18+
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]
19+
20+
21+
class Solution:
22+
def rotate(self, matrix: List[List[int]]) -> None:
23+
n = len(matrix)
24+
25+
# Step 1: Transpose matrix (swap elements across diagonal)
26+
for i in range(n):
27+
for j in range(i, n):
28+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
29+
30+
# Step 2: Reverse each row
31+
for i in range(n):
32+
matrix[i].reverse()
File renamed without changes.
File renamed without changes.
File renamed without changes.
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(m * n)
3+
- m์€ root ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ์ˆ˜, n์€ subRoot ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ์ˆ˜
4+
5+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(h)
6+
- h๋Š” root ํŠธ๋ฆฌ์˜ ๋†’์ด
7+
'''
8+
class TreeNode:
9+
def __init__(self, val=0, left=None, right=None):
10+
self.val = val
11+
self.left = left
12+
self.right = right
13+
from typing import Optional
14+
15+
16+
class Solution:
17+
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
18+
if not subRoot:
19+
return True
20+
21+
if not root:
22+
return False
23+
24+
return self.isSameTree(root, subRoot) or self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)
25+
26+
def isSameTree(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
27+
if not root1 and not root2:
28+
return True
29+
30+
if not root1 or not root2:
31+
return False
32+
33+
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
3+
- n์€ ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ ์ˆ˜
4+
5+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(h)
6+
- h๋Š” ํŠธ๋ฆฌ์˜ ๋†’์ด
7+
'''
8+
from typing import Optional
9+
10+
# Definition for a binary tree node.
11+
class TreeNode:
12+
def __init__(self, val=0, left=None, right=None):
13+
self.val = val
14+
self.left = left
15+
self.right = right
16+
17+
class Solution:
18+
def isValidBST(self, root: Optional[TreeNode]) -> bool:
19+
def validate(node, min_val, max_val):
20+
if not node:
21+
return True
22+
23+
# ํ˜„์žฌ ๋…ธ๋“œ ๊ฐ’์ด ํ—ˆ์šฉ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด False
24+
if node.val <= min_val or node.val >= max_val:
25+
return False
26+
27+
# ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋Š” ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ์ž‘์•„์•ผ ํ•˜๊ณ , ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋Š” ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ์ปค์•ผ ํ•จ
28+
return validate(node.left, min_val, node.val) and validate(node.right, node.val, max_val)
29+
30+
# ์ดˆ๊ธฐ ๋ฒ”์œ„๋Š” ์ „์ฒด ์ •์ˆ˜ ๋ฒ”์œ„๋กœ ์„ค์ •
31+
return validate(root, float('-inf'), float('inf'))
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
ย (0)