-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Chaedie] Week 11 #1032
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
[Chaedie] Week 11 #1032
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a6d7581
feat: [Week 11]
Chaedie 9230074
Merge branch 'DaleStudy:main' into main
Chaedie 73aba40
fix: graph-valid-tree
Chaedie 968d494
fix: add complexity for graph-valid-tree
Chaedie e5cc679
Merge branch 'DaleStudy:main' into main
Chaedie 9377f2a
Merge branch 'main' of https://github.com/Chaedie/leetcode-study
Chaedie 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,39 @@ | ||
# 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 | ||
|
||
""" | ||
Solution: DFS | ||
1) dfs 로 left, right 각각의 max 값을 구한다. | ||
2) maxSum 을 업데이트하고, | ||
3) return value 로는 leftMax 또는 rightMax 와의 합만 리턴한다. | ||
(left, right 를 둘 다 포함하는 경우와 둘 중 하나만 선택하는 경우를 나눔) | ||
|
||
Time: O(n) | ||
Space: O(n) | ||
|
||
""" | ||
|
||
|
||
class Solution: | ||
def maxPathSum(self, root: Optional[TreeNode]) -> int: | ||
maxSum = root.val | ||
|
||
def dfs(root): | ||
nonlocal maxSum | ||
if not root: | ||
return 0 | ||
|
||
leftMax = dfs(root.left) | ||
rightMax = dfs(root.right) | ||
leftMax = max(leftMax, 0) | ||
rightMax = max(rightMax, 0) | ||
|
||
maxSum = max(maxSum, root.val + leftMax + rightMax) | ||
return root.val + max(leftMax, rightMax) | ||
|
||
dfs(root) | ||
return maxSum |
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,39 @@ | ||
""" | ||
Conditions of Valid Tree | ||
1) no Loop | ||
2) all nodes has to be connected | ||
|
||
Time: O(node + edge) | ||
Space: O(node + edge) | ||
""" | ||
|
||
|
||
class Solution: | ||
def validTree(self, n: int, edges: List[List[int]]) -> bool: | ||
if not n: | ||
return True | ||
if len(edges) != n - 1: | ||
return False | ||
|
||
# Make Graph | ||
graph = {i: [] for i in range(n)} | ||
for n1, n2 in edges: | ||
graph[n1].append(n2) | ||
graph[n2].append(n1) | ||
|
||
# loop check | ||
visit = set() | ||
|
||
def dfs(i, prev): | ||
if i in visit: | ||
return False | ||
|
||
visit.add(i) | ||
for j in graph[i]: | ||
if j == prev: | ||
continue | ||
if not dfs(j, i): | ||
return False | ||
return True | ||
|
||
return dfs(0, None) and n == len(visit) |
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,23 @@ | ||
""" | ||
Solution: BFS | ||
Time: O(n) | ||
Space: O(n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
if not root: | ||
return 0 | ||
|
||
q = deque([root]) | ||
maxLevel = 0 | ||
while q: | ||
maxLevel += 1 | ||
for i in range(len(q)): | ||
node = q.popleft() | ||
if node.left: | ||
q.append(node.left) | ||
if node.right: | ||
q.append(node.right) | ||
return maxLevel |
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,19 @@ | ||
""" | ||
Time: O(n log(n)) | ||
Space: O(n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def merge(self, intervals: List[List[int]]) -> List[List[int]]: | ||
intervals.sort(key=lambda x: x[0]) | ||
result = [intervals[0]] | ||
|
||
for start, end in intervals[1:]: | ||
prev = result[-1] | ||
|
||
if prev[0] <= start <= prev[1]: | ||
result[-1][1] = max(prev[1], end) | ||
else: | ||
result.append([start, end]) | ||
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,42 @@ | ||
""" | ||
가장 뒤부터 돌아오는 방법을 찾아야한다. | ||
1) 재귀 스택 방식 | ||
2) 새로운 리스트를 만드는 방법 | ||
3) two pointer 로 반 잘라서 reverse 한 뒤 merge | ||
""" | ||
|
||
""" | ||
Solution: 3) Two pointer | ||
Time: O(n) | ||
Space: O(1) | ||
""" | ||
|
||
|
||
class Solution: | ||
def reorderList(self, head: Optional[ListNode]) -> None: | ||
|
||
# 절반 자르기 | ||
slow, fast = head, head.next | ||
while fast and fast.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
|
||
# Reverse | ||
second = slow.next | ||
slow.next = None | ||
prev = None | ||
while second: | ||
tmp = second.next | ||
second.next = prev | ||
prev = second | ||
second = tmp | ||
|
||
# Merge | ||
first = head | ||
second = prev | ||
while second: | ||
tmp1, tmp2 = first.next, second.next | ||
first.next = second | ||
second.next = tmp1 | ||
first = tmp1 | ||
second = tmp2 |
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.
이 문제에는 트리의 성질을 이용한 조기종료 조건이 하나 더 숨어있어요.
시간이 괜찮으시다면 한번 찾아보시는것도 좋을 것 같습니다!
그리고 이 문제만 복잡도 분석이 빠져있는데, 혹시 빠뜨리신걸까 해서 말씀드려요!
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.
감사합니다.. ! 다른 분들 코드 참고를 많이 해서 예뻐진것 같습니다.. 😀
간선의 갯수 < 노드 갯수 - 1
일 경우 연결되지 않은 그래프간선의 갯수 > 노드 갯수 - 1
일 경우 순환이 있는 그래프라는 조기 종료 조건이 있네요..! 감사합니다.