-
-
Notifications
You must be signed in to change notification settings - Fork 247
[std-freejia] week04 solutions #1822
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 all commits
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,54 @@ | ||||||||||
/** | ||||||||||
* BFS 는 너비 우선이므로, 가장 얕은 깊이로 목표 금액을 만족하면 '최소 동전 개수'를 사용한 것입니다. | ||||||||||
* 너비 == 사용한 동전 개수 | ||||||||||
* | ||||||||||
* Queue<Integer> 동전 금액 '합' 을 관리합니다 | ||||||||||
* | ||||||||||
* 목표 금액이 amount 일 때, | ||||||||||
* 0 부터 amount 까지의 합을 체크할 boolean[] 을 활용합니다. | ||||||||||
* 합계 금액이 '이미 만들어본 합' 이면 boolean[] 에 체크하여 중복방문을 방지하기 위함. | ||||||||||
* | ||||||||||
*/ | ||||||||||
class Solution { | ||||||||||
public int coinChange(int[] coins, int amount) { | ||||||||||
if (amount == 0) return 0; // 목표 금액 0인 경우, '0개' 리턴 | ||||||||||
|
||||||||||
Arrays.sort(coins); // 작은 동전부터 더해본다 | ||||||||||
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. 지금의 0-1 BFS에서 정렬은 이점이 없어 보이고 생략 가능합니다. |
||||||||||
|
||||||||||
// 이미 만들어본 '합' 은 지나가도록. | ||||||||||
boolean[] visitedAmount = new boolean[amount + 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.
Suggested change
|
||||||||||
// 합 | ||||||||||
Queue<Integer> amountQueue = new ArrayDeque<>(); | ||||||||||
|
||||||||||
amountQueue.add(0); // 시작 금액 0 | ||||||||||
visitedAmount[0] = true; | ||||||||||
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.
Suggested change
|
||||||||||
|
||||||||||
int coinCount = 0; // 사용한 동전 개수 | ||||||||||
|
||||||||||
while(!amountQueue.isEmpty()) { | ||||||||||
coinCount++; // 사용한 동전 개수 1개 증가 | ||||||||||
|
||||||||||
int queueSize = amountQueue.size(); | ||||||||||
|
||||||||||
for(int i = 0; i < queueSize; i++) { | ||||||||||
// 현재 금액 합 | ||||||||||
int currentAmount = amountQueue.poll(); | ||||||||||
|
||||||||||
// 모든 코인을 더해본다 | ||||||||||
for(int coin : coins) { | ||||||||||
// 코인을 더해 본 합. | ||||||||||
int tempAmount = currentAmount + coin; | ||||||||||
|
||||||||||
if (tempAmount == amount) return coinCount; // 최소 동전 개수 찾음 | ||||||||||
|
||||||||||
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. 가지치기를 위해 (#1699 (comment)) 정렬 상태를 이용할 수는 있겠네요.
Suggested change
|
||||||||||
// 처음 만든 합 이고, 목표 금액보다 적은 합이라면, | ||||||||||
if (tempAmount < amount && !visitedAmount[tempAmount]) { | ||||||||||
visitedAmount[tempAmount] = true; | ||||||||||
Comment on lines
+45
to
+46
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.
Suggested change
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. 꼼꼼한 리뷰 감사합니다! 다시 생각해보겠습니다. |
||||||||||
amountQueue.add(tempAmount); | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
return -1; | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* 최대 depth 까지 가봐야 하므로 DFS 탐색 이용 | ||
* | ||
* 모든 노드를 순회해야 하므로 시간복잡도 O(N) | ||
* 재귀 호출 스택 프레임 (= 트리 최대 높이 height) 만큼 공간복잡도 O(H) | ||
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. 이 문제에서는 |
||
* | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
public int maxDepth(TreeNode root) { | ||
if (root == null) return 0; // 자식이 없으면 종료 | ||
|
||
int left = maxDepth(root.left); | ||
int right = maxDepth(root.right); | ||
return Math.max(left, right) + 1; // 루트는 기본 1층이니까 1 더한다 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||||
/** | ||||||||
* 고생한 이유: ListNode 의 시작점을 따로 둬야 하는 것! | ||||||||
* | ||||||||
* ListNode dummy = new ListNode(0); // 시작점 | ||||||||
* ListNode output = dummy; // 시작점 뒤에 실제 작업용 포인터 | ||||||||
* | ||||||||
* | ||||||||
* Definition for singly-linked list. | ||||||||
* public class ListNode { | ||||||||
* int val; | ||||||||
* ListNode next; | ||||||||
* ListNode() {} | ||||||||
* ListNode(int val) { this.val = val; } | ||||||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||||||||
* } | ||||||||
*/ | ||||||||
class Solution { | ||||||||
public static ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||||||||
// 길이가 둘다 null 이면, return null | ||||||||
if (list1 == null && list2 == null) return null; | ||||||||
Comment on lines
+19
to
+20
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. 안녕하세요 리뷰어님 코멘트 감사합니다! |
||||||||
|
||||||||
ListNode dummy = new ListNode(0); // 시작점 | ||||||||
ListNode output = dummy; // 시작점 뒤에 실제 작업용 포인터 | ||||||||
Comment on lines
+22
to
+23
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.
Suggested change
둘다 똑같은 시작점이므로 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. dummy 는 고정된 시작점이고, output 은 이어붙일 포인터라서 따로 관리했습니다. 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. 제 코드에서는 더 나은 네이밍이 필요한 것 같네요. 리뷰 감사합니다! |
||||||||
|
||||||||
while(list1 != null && list2 != null) { | ||||||||
|
||||||||
if (list1.val < list2.val) { | ||||||||
output.next = list1; | ||||||||
list1 = list1.next; // list1 포인터 이동 | ||||||||
} else { | ||||||||
output.next = list2; | ||||||||
list2 = list2.next; // list2 포인터 이동 | ||||||||
} | ||||||||
output = output.next; // output 포인터 이동 | ||||||||
} | ||||||||
|
||||||||
if (list1 != null) { | ||||||||
output.next = list1; | ||||||||
} | ||||||||
if (list2 != null) { | ||||||||
output.next = list2; | ||||||||
} | ||||||||
|
||||||||
return dummy.next; | ||||||||
yhkee0404 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
} | ||||||||
} |
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.
사용한 동전 개수
또는최소 동전 개수
는 너비 우선 탐색 트리의너비
가 아니라깊이
또는 Level 아닐까요?너비 즉 연결된 간선 수는 총 동전 개수
coins.length
같습니다.그래서 시간 복잡도는
O(V+E) = O(amount + amount * coins.length) = O(amount * coins.length)
같네요.