Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 20 additions & 0 deletions find-minimum-in-rotated-sorted-array/dolphinflow86.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

혹시 pivot을 (right + left) // 2 가 아닌 left + (right - left) // 2 로 하신 이유가 있으실까요?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Binary Search
  • 설명: 배열을 이진 탐색으로 절반으로 줄이며 회전된 정렬 배열에서 최솟값의 위치를 찾는다. 각 순환에서 중간 값과 끝 값을 비교해 좌측 또는 우측 구간으로 범위를 축소한다.

📊 시간/공간 복잡도 분석

복잡도
Time O(log n)
Space O(1)

피드백: 구현은 화살표의 진행 방향을 결정하는 비교로 왼쪽/오른쪽의 절댓값 구간을 반으로 나눠 탐색한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 1) Use binary search to halves to search range. Each iteration,
# compare with nums[pivot] and nums[right] to see which side has a smaller range.
# Firstly I compare nums[left] with nums[right] so I got the wrong results but end up with the right solution.
# TC: O(logN) where N is the length of the nums array
# SC: O(1)

class Solution:
def findMin(self, nums: List[int]) -> int:
n = len(nums)
left = 0
right = n-1

while left < right:
pivot = left + (right - left) // 2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

pivot = left + (right - left) // 2로 오버플로를 방어하신 부분이 인상깊었습니다!

풀이를 보고 문득 Python에서는 오버플로가 어떻게 되는지 궁금해져서 찾아보니, Python의 int는 임의정밀도(arbitrary precision)라 값이 아무리 커져도 오버플로우가 나지 않는다고 하더라고요. 그래서 이 경우엔 (left + right) // 2로 단순하게 써도 괜찮지 않을까 싶은데, 어떻게 생각하시나요?

반대로 저는 Java인데도 오버플로우 방어 없이 (left + right) / 2로 풀었더라고요 😅

@alphaorderly alphaorderly Jul 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

이거 근데

  • $$n == nums.length$$
  • $$1 \le n \le 5000$$

nums 배열 길이가 길어야 5,000이라서
left + right 의 최대 값이 10,000이라서요
오버플로우 애초에 신경 안써도 될거에요

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

두 분 다 좋은 코멘트 감사합니다!

제가 예전에는 c++로 주로 풀었다보니 습관적으로 저렇게 썼던 것 같아요.
문제 조건 상 저렇게 안해도 되겠네요!
그리고 파이썬은 int가 임의정밀도라는 사실 새로 알게 되었습니다. 감사합니다 :)

@alphaorderly alphaorderly Jul 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

생각나서 하나 덧붙히자면 보통 오버플로우 일어나서 관리해야 하는 문제들은 10^9 + 7 로 나머지연산 해서 리턴하라고 하더라고요!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

실질적으로 left, right의 중간에 해당하는 mid 값이 들어가서 mid 같은 변수명을 쓰는게 어떨까 싶습니다. 이후 역할로서 pivot으로 사용하는 거라서요.

if nums[pivot] < nums[right]:
right = pivot
else:
left = pivot + 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

좀더 최적화 한다면 이 로직을 추가해볼 수 있을 거 같습니다.

if nums[left] < nums[pivot] < nums[right]:
                return nums[left]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@parkhojeong 오.. 생각하지 못했는데 좋은 최적화 포인트네요!
알려주셔서 감사합니다.


return nums[right]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: right 보단 left가 약간 더 자연스러운(?) 개인적인 느낌이 있는 거 같습니다.

Suggested change
return nums[right]
return nums[left]

20 changes: 20 additions & 0 deletions maximum-depth-of-binary-tree/dolphinflow86.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

이거 depth 패러미터 주지 않고 maxDepth 만으로도 구현 가능하세요! 스스로 재귀해서 하면 코드도 엄청 줄일수 있어요, 성능도 같고요

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

bottom up 방식으로 depth를 더하면서 올라오면 간단하게 끝나는군요
리뷰 감사합니다!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Depth-First Search, Binary Search
  • 설명: 주요 로직이 DFS로 트리를 깊이 우선 순회하며 각 가지의 깊이를 탐색하고 최댓값을 반환하기 때문에 DFS 패턴에 해당합니다. 트리의 탐색과 깊이 관리로 알고리즘이 구성되어 있습니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(N) O(n)
Space O(H) O(h)

피드백: 리프까지 방문해야 하므로 노드 수에 비례하는 시간과 재귀 호출 스택의 최대 깊이를 공간복잡도로 가진다.

개선 제안: 현재 구현이 적절해 보입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 1) Use DFS to traverse down to the leaf node. Keep track of depth and return each node's maximum depth of each subtree to the parent node.
# TC: O(N) where N is the number of node in the binary tree
# SC: O(H) where H is the height of the binary tree
# 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 dfs(self, node: Optional[TreeNode], depth: int) -> int:
if not node:
return depth

left = self.dfs(node.left, depth + 1)
right = self.dfs(node.right, depth + 1)
return max(left, right)
Comment thread
dolphinflow86 marked this conversation as resolved.
Outdated

def maxDepth(self, root: Optional[TreeNode]) -> int:
return self.dfs(root, 0)
27 changes: 27 additions & 0 deletions merge-two-sorted-lists/dolphinflow86.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Merge Sort (not listed), Linked List
  • 설명: 두 포인터를 사용해 두 연결 리스트를 병합하는 방식으로, 작은 값의 노드를 차례로 선택해 새 리스트를 구성합니다. 리스트를 순회하며 한쪽이 끝나면 나머지를 연결하고 시간 복잡도는 O(N+M)입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(m + n)
Space O(1)

피드백: 두 연결 리스트를 순차적으로 병합하며, 추가적인 메모리 할당 없이 노드 연결만 수행한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 1) Use a dummy node and connect the smaller node to the merged list while iterating through both lists.
# TC: O(N + M) where N is the length of list1 and M is the length of list2.
# SC: O(1)
#
# 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 = ListNode()
curr = dummy

while list1 and list2:
if list1.val < list2.val:
curr.next = list1
list1 = list1.next
else:
curr.next = list2
list2 = list2.next
curr = curr.next

curr.next = list1 if list1 else list2

return dummy.next

@parkhojeong parkhojeong Jul 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

dummy 도 괜찮은데 의미를 조금 더 담은 네이밍이면 어떨까 싶습니다.

Loading