-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
[Lyla] Week 13 #1074
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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. 파이썬 언어 특징이 정말 엄청나네요 ㅎㅎ 테스트 케이스까지 잘 구현해 주셔서 감사합니다! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
공간 복잡도를 O(n)에서 O(h) h는 재귀 스택의 높이, 로 향상시키려면 어떻게 할 수 있을까요?