-
-
Notifications
You must be signed in to change notification settings - Fork 245
[devyejin] WEEK 04 solutions #1824
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
Changes from 4 commits
855e2fc
7fd579d
3bdc80f
065acb2
4bd55ba
80955d9
56cd324
08cfbf7
f93ae6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 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 | ||
devyejin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Solution: | ||
def maxDepth(self, root: Optional[TreeNode]) -> int: | ||
if not root: | ||
return 0 | ||
|
||
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 | ||
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. 시잔&공간복잡도를 일관적으로 작성해보시는 것도 추천드립니다. 자꾸 하다보니 복잡도 계산이 전보다 쉬워지더라구요. 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. 맞아요! 조언 감사합니다~ 앞으로 꾸준히 작성해볼게요 🥰 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
class Solution: | ||
def maxSubArray(self, nums: list[int]) -> int: | ||
dp = [0] * len(nums) | ||
dp[0] = nums[0] | ||
current_sum = nums[0] | ||
max_sum = nums[0] | ||
|
||
for i in range(1, len(nums)): | ||
dp[i] = max(nums[i], dp[i - 1] + nums[i]) | ||
current_sum = max(nums[i], current_sum + nums[i]) | ||
max_sum = max(current_sum, max_sum) | ||
|
||
return max(dp) | ||
return max_sum | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Definition for singly-linked list. | ||
class ListNode: | ||
def __init__(self, val=0, next=None): | ||
self.val = val | ||
self.next = next | ||
|
||
|
||
""" | ||
time : O(m+n) | ||
space : O(1) | ||
""" | ||
|
||
|
||
class Solution: | ||
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: | ||
dummy = ListNode(None) | ||
|
||
|
||
node = dummy | ||
|
||
while list1 and list2: | ||
if list1.val <= list2.val: | ||
node.next = list1 | ||
list1 = list1.next | ||
else: | ||
node.next = list2 | ||
list2 = list2.next | ||
node = node.next | ||
node.next = list1 or list2 | ||
|
||
return dummy.next |
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.
깊은 트리에서는 Python recursion limit 문제 발생 가능 → BFS나 stack 기반 DFS 방식도 고려하면 좋음.
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.
습관적으로 DFS 쓰게되는데 다른 방식 추천 감사합니다!