-
-
Notifications
You must be signed in to change notification settings - Fork 245
[yeoju] Week 13 #1085
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
[yeoju] Week 13 #1085
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ec3553f
FEAT : add solution (maximum-depth-of-binary-tree)
crumbs22 d34c7da
Merge branch 'DaleStudy:main' into main
crumbs22 ebf843a
FEAT: solve meeting-rooms
crumbs22 5fb49da
FIX : lint add
crumbs22 d3dfa3c
CHORE : 주석 추가가
crumbs22 9faf96b
FEAT: solve lowest-common-ancestor-of-a-binary-search-tree
crumbs22 eb7d9aa
CHORE: add newline
crumbs22 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,22 @@ | ||
''' | ||
TC: O(h) (h는 트리의 높이) | ||
SC: O(1) | ||
풀이 방법: 이진 탐색 트리의 성질을 이용해서 | ||
p,q의 값이 현재 노드 사이에 있거나 p,q의 값 중 하나라도 현재 노드와 같을 때까지 탐색한다 | ||
''' | ||
# 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': | ||
while root: | ||
if p.val < root.val and q.val < root.val: | ||
root = root.left | ||
elif p.val > root.val and q.val > root.val: | ||
root = root.right | ||
else: | ||
return root |
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,12 @@ | ||
''' | ||
TC: O(n) | ||
SC: O(n) | ||
''' | ||
|
||
class Solution: | ||
def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
# 재귀함수 호출 시 노드가 존재하지 않을 때 탈출한다 | ||
if not root: | ||
return 0 | ||
# root를 기준으로 왼쪽 서브트리, 오른쪽 서브트리를 재귀호출로 쌓아놓고 리프노드부터 세면서 트리의 깊이를 구한다 | ||
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 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,32 @@ | ||
''' | ||
TC: O(nlogn) | ||
SC: O(1) | ||
풀이 방법: 구간 중 첫번째 인자에 대해 정렬한 뒤, 이웃한 인자에 대해서만 겹치는지 확인한다 | ||
시간 복잡도는 정렬에 드는 nlogn만큼 필요하다 | ||
''' | ||
|
||
from typing import ( | ||
List, | ||
) | ||
from lintcode import ( | ||
Interval, | ||
) | ||
""" | ||
Definition of Interval: | ||
class Interval(object): | ||
def __init__(self, start, end): | ||
self.start = start | ||
self.end = end | ||
""" | ||
|
||
class Solution: | ||
""" | ||
@param intervals: an array of meeting time intervals | ||
@return: if a person could attend all meetings | ||
""" | ||
def can_attend_meetings(self, intervals: List[Interval]) -> bool: | ||
intervals.sort(key=lambda x: x.start) | ||
for i in range(1, len(intervals)): | ||
if intervals[i - 1].end > intervals[i].start: | ||
return False | ||
return True |
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.
TC: O(n) 최악의 경우 모든 노드를 방문
SC: O(h) 재귀 스택의 높이 만큼의 공간
이 되지 않을까요? :)