Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions maximum-depth-of-binary-tree/devyejin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Definition for a binary tree node.
Copy link
Contributor

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 방식도 고려하면 좋음.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

습관적으로 DFS 쓰게되는데 다른 방식 추천 감사합니다!

# 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:
if not root:
return 0

return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

시잔&공간복잡도를 일관적으로 작성해보시는 것도 추천드립니다. 자꾸 하다보니 복잡도 계산이 전보다 쉬워지더라구요.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞아요! 조언 감사합니다~ 앞으로 꾸준히 작성해볼게요 🥰

9 changes: 5 additions & 4 deletions maximum-subarray/devyejin.py
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

30 changes: 30 additions & 0 deletions merge-two-sorted-lists/devyejin.py
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dummy = ListNode(None) → dummy = ListNode() 추천
ListNode 기본값이 val=0인데 굳이 None을 주면 타입 일관성이 깨질 수 있음

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분에 대해 생각을 못했는데 꼼꼼한 리뷰 감사합니다! 😄


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