-
-
Notifications
You must be signed in to change notification settings - Fork 247
[jongwanra] WEEK 04 Solutions #1819
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
Changes from all commits
Commits
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,60 @@ | ||
""" | ||
[Problem] | ||
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ | ||
|
||
Return maximum depth of binary tree | ||
0 < the number of nodes < 10,000 | ||
|
||
[Brainstorming] | ||
DFS를 이용해서 구해보자. | ||
이진 트리에서 전체 탐색을 DFS로 진행했을 떄 시간 복잡도는 O(N)으로 판단된다. | ||
|
||
[Complexity] | ||
N is the number of nodes | ||
Time: O(N) | ||
Space: O(N) | ||
-> node의 개수 만큼 call stack이 쌓임 | ||
""" | ||
|
||
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 maxDepth(self, root: Optional[TreeNode]) -> int: | ||
answer = 0 | ||
|
||
def dfs(depth, node:Optional[TreeNode])->None: | ||
nonlocal answer | ||
if not node.left and not node.right: | ||
answer = max(answer, depth) | ||
return | ||
|
||
if node.left: | ||
dfs(depth + 1, node.left) | ||
if node.right: | ||
dfs(depth + 1, node.right) | ||
|
||
if not root: | ||
return answer | ||
dfs(1, root) | ||
return answer | ||
|
||
|
||
class AnotherSolution: | ||
""" | ||
ref: leetcode | ||
|
||
[Complexity] | ||
Time: O(N) | ||
Space: O(N) | ||
""" | ||
def maxDepth(self, root:Optional[TreeNode])-> int: | ||
if not root: | ||
return 0 | ||
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) | ||
|
||
|
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,67 @@ | ||
""" | ||
[Problem] | ||
https://leetcode.com/problems/merge-two-sorted-lists/description/ | ||
|
||
두 정렬된 링크드 리스트의 Head가 주어진다. | ||
두 리스트를 하나의 정렬된 리스트로 합치시오. | ||
merged linked list의 Head를 반환하자. | ||
|
||
[Brainstorming] | ||
두 개의 Sorted Linked Listd의 Head가 주어졌을 떄, 이미 정렬이 되어 있기 떄문에 | ||
반복문을 총 2번 순회하여 하나의 Merged Linked List를 만들 수 있다. | ||
|
||
[Complexity] | ||
N: list1.length, M: list2.length | ||
Time: O(N + M) | ||
Space: O(N + M) | ||
|
||
[Todo] | ||
- 재귀적으로 풀어보기. | ||
|
||
""" | ||
from typing import Optional, List | ||
|
||
|
||
# Definition for singly-linked list. | ||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
class Solution: | ||
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: | ||
dummy_head = ListNode(0) | ||
current_node = dummy_head | ||
while list1 and list2: | ||
if list1.val > list2.val: | ||
current_node.next = ListNode(list2.val) | ||
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. 기존 노드를 재사용하지 않고 새 노드를 계속 생성 → 메모리 낭비 |
||
list2 = list2.next | ||
else: | ||
current_node.next = ListNode(list1.val) | ||
list1 = list1.next | ||
current_node = current_node.next | ||
|
||
current_node.next = list1 or list2 | ||
|
||
return dummy_head.next | ||
|
||
|
||
def generate(list:List[int])->Optional[ListNode]: | ||
dummy_head = ListNode(0) | ||
current_node = dummy_head | ||
for x in list: | ||
current_node.next = ListNode(x) | ||
current_node = current_node.next | ||
return dummy_head.next | ||
|
||
def print_list(node:Optional[ListNode])->None: | ||
list = [] | ||
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. list라는 변수명이 Python 내장 타입과 충돌 |
||
while node: | ||
list.append(node.val) | ||
node = node.next | ||
print(list) | ||
|
||
sol = Solution() | ||
print_list(sol.mergeTwoLists(generate([1,2,4]), generate([1,3,4]))) | ||
|
||
|
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
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.
이 부분은 문제 없지만, root가 없는 경우는 이미 위에서 처리했기 때문에 if not node: 로 처리해도 더 범용적