-
-
Notifications
You must be signed in to change notification settings - Fork 361
[freemjstudio] WEEK 04 Solutions #2752
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 5 commits
f9b7d6b
4136b95
ba56afa
bf46998
61ec21a
9bdac9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: usable_coins를 모아 두고 각 금액에 대해 이전 상태를 이용해 최소 개수를 갱신한다. amount의 크기와 사용 가능한 동전 수에 따라 시간 복잡도가 결정된다. 개선 제안: 고려해볼 만한 대안: 초기 금액이 큰 경우 BFS/동전 교환 문제의 표준 DP 방식으로 구현하면 직관적이며, 불필요한 루프를 줄여 상수 계수도 줄일 수 있습니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| class Solution: | ||
| def coinChange(self, coins: List[int], amount: int) -> int: | ||
| if amount == 0: | ||
| return 0 | ||
| INF = int(1e9) | ||
| # dp[n] 은 n 원을 만들기 위한 최소한의 동전 개수를 저장한다. | ||
| dp = [INF] * (amount + 1) | ||
| usable_coins = [] | ||
| for coin in coins: | ||
| if coin == amount: | ||
| return 1 | ||
| if coin < amount: | ||
| dp[coin] = 1 # dp[3] = 1 | ||
| usable_coins.append(coin) | ||
|
|
||
| # dp 를 채워나가는 과정 | ||
| for i in range(1, amount+1): | ||
| for coin in usable_coins: | ||
| if (i - coin) >= 0 and dp[i - coin] != INF: | ||
| dp[i] = min(dp[i - coin] + 1, dp[i]) | ||
|
|
||
| return -1 if dp[amount] == INF else dp[amount] |
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 정렬 상태의 일부 특성을 이용해 중앙값과 끝 값 비교로 범위를 절반으로 축소한다. 개선 제안: 현재 구현은 min_value 초기화가 상수로 잡혀 있지만, 일반적으로 nums[mid]와 nums[right] 비교를 통해 더 명확하게 구현 가능하며, 중복 원소 처리까지 고려하면 안전합니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Solution: | ||
| def findMin(self, nums: List[int]) -> int: | ||
| min_value = 5000 | ||
| # O(log n) 이므로 binary search | ||
| if len(nums) == 1: | ||
| return nums[0] | ||
| left = 0 | ||
| right = len(nums) - 1 | ||
|
|
||
| while left <= right: | ||
| mid = (left + right) // 2 | ||
| min_value = min(nums[mid], min_value) | ||
|
|
||
|
freemjstudio marked this conversation as resolved.
|
||
| if nums[mid] > nums[right]: | ||
| left = mid + 1 | ||
| else: | ||
| right = mid - 1 | ||
|
|
||
| return min_value | ||
|
freemjstudio marked this conversation as resolved.
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 자식 노드를 재귀적으로 탐색해 좌우 깊이의 최댓값에 +1을 더한다. 스택 깊이는 트리의 높이에 비례한다. 개선 제안: 꼭 재귀 대신 명시적 스택으로 구현해 시스템 스택 오버플로를 피하는 방법도 있음.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # 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: | ||
| if root is None: | ||
| return 0 | ||
|
|
||
| left = self.maxDepth(root.left) | ||
| right = self.maxDepth(root.right) | ||
| return max(left, right) + 1 |
|
Contributor
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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # 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() | ||
| merged_list = dummy | ||
|
freemjstudio marked this conversation as resolved.
|
||
|
|
||
| while list1 is not None and list2 is not None: | ||
| if list1.val <= list2.val: | ||
| merged_list.next = list1 | ||
| list1 = list1.next | ||
| else: | ||
| merged_list.next = list2 | ||
| list2 = list2.next | ||
| # merged_list 포인터를 한칸 이동시켜서 다음 노드 붙일 준비 | ||
| merged_list = merged_list.next | ||
|
|
||
| # 남아 있는 노드 붙이기 | ||
| merged_list.next = list1 if list1 is not None else list2 | ||
|
freemjstudio marked this conversation as resolved.
|
||
|
|
||
| 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.
가독성 좋게 잘 풀어주신 거 같습니다.