Skip to content

[Lyla] Week 13 #1074

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 8, 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
32 changes: 32 additions & 0 deletions insert-interval/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
시간 복잡도: O(n)
공간 복잡도: O(n)
'''
from typing import List


class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
n = len(intervals)
start, end = newInterval
i = 0
result = []

# before merging
while i < n and intervals[i][1] < start:
result.append(intervals[i])
i += 1

# merge
while i < n and intervals[i][0] <= end:
start = min(start, intervals[i][0])
end = max(end, intervals[i][1])
i += 1
result.append([start, end])

# after merging
while i < n:
result.append(intervals[i])
i += 1

return result
25 changes: 25 additions & 0 deletions kth-smallest-element-in-a-bst/pmjuu.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공간 복잡도를 O(n)에서 O(h) h는 재귀 스택의 높이, 로 향상시키려면 어떻게 할 수 있을까요?

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
시간 복잡도: O(n)
공간 복잡도: O(n)
'''
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 kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
def inorder_traversal(node):
if node:
inorder_traversal(node.left)
values.append(node.val)
inorder_traversal(node.right)

values = []
inorder_traversal(root)

return values[k - 1]
22 changes: 22 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/pmjuu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'''
시간 복잡도: O(h) (h = 트리 높이)
공간 복잡도: O(1)
'''
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None

class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
node = root

while node:
if p.val < node.val and q.val < node.val:
node = node.left
elif node.val < p.val and node.val < q.val:
node = node.right
else:
return node
52 changes: 52 additions & 0 deletions meeting-rooms/pmjuu.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

파이썬 언어 특징이 정말 엄청나네요 ㅎㅎ 테스트 케이스까지 잘 구현해 주셔서 감사합니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'''
시간 복잡도: O(n log n)
공간 복잡도: O(1)
'''
import unittest
from typing import List

class Interval(object):
def __init__(self, start, end):
self.start = start
self.end = end

class Solution:
def can_attend_meetings(self, intervals: List[Interval]) -> bool:
intervals.sort(key=lambda x:x.start)

for i in range(len(intervals) - 1):
if intervals[i + 1].start < intervals[i].end:
return False

return True

class TestCanAttendMeetings(unittest.TestCase):
def setUp(self):
self.solution = Solution()

def test_case_1(self):
intervals = [Interval(0, 30), Interval(5, 10), Interval(15, 20)]
self.assertFalse(self.solution.can_attend_meetings(intervals))

def test_case_2(self):
intervals = [Interval(5, 8), Interval(9, 15)]
self.assertTrue(self.solution.can_attend_meetings(intervals))

def test_case_3(self):
intervals = []
self.assertTrue(self.solution.can_attend_meetings(intervals))

def test_case_4(self):
intervals = [Interval(1, 5)]
self.assertTrue(self.solution.can_attend_meetings(intervals))

def test_case_5(self):
intervals = [Interval(0, 5), Interval(5, 10), Interval(10, 15)]
self.assertTrue(self.solution.can_attend_meetings(intervals))

def test_case_6(self):
intervals = [Interval(1, 3), Interval(2, 6), Interval(8, 10)]
self.assertFalse(self.solution.can_attend_meetings(intervals))

if __name__ == "__main__":
unittest.main()