Skip to content

Commit c56b100

Browse files
authored
Merge pull request #1822 from std-freejia/main
[std-freejia] week04 solutions
2 parents f22d2d0 + 3c9a30f commit c56b100

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

โ€Žcoin-change/std-freejia.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* BFS ๋Š” ๋„ˆ๋น„ ์šฐ์„ ์ด๋ฏ€๋กœ, ๊ฐ€์žฅ ์–•์€ ๊นŠ์ด๋กœ ๋ชฉํ‘œ ๊ธˆ์•ก์„ ๋งŒ์กฑํ•˜๋ฉด '์ตœ์†Œ ๋™์ „ ๊ฐœ์ˆ˜'๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
3+
* ๋„ˆ๋น„ == ์‚ฌ์šฉํ•œ ๋™์ „ ๊ฐœ์ˆ˜
4+
*
5+
* Queue<Integer> ๋™์ „ ๊ธˆ์•ก 'ํ•ฉ' ์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค
6+
*
7+
* ๋ชฉํ‘œ ๊ธˆ์•ก์ด amount ์ผ ๋•Œ,
8+
* 0 ๋ถ€ํ„ฐ amount ๊นŒ์ง€์˜ ํ•ฉ์„ ์ฒดํฌํ•  boolean[] ์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.
9+
* ํ•ฉ๊ณ„ ๊ธˆ์•ก์ด '์ด๋ฏธ ๋งŒ๋“ค์–ด๋ณธ ํ•ฉ' ์ด๋ฉด boolean[] ์— ์ฒดํฌํ•˜์—ฌ ์ค‘๋ณต๋ฐฉ๋ฌธ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•จ.
10+
*
11+
*/
12+
class Solution {
13+
public int coinChange(int[] coins, int amount) {
14+
if (amount == 0) return 0; // ๋ชฉํ‘œ ๊ธˆ์•ก 0์ธ ๊ฒฝ์šฐ, '0๊ฐœ' ๋ฆฌํ„ด
15+
16+
Arrays.sort(coins); // ์ž‘์€ ๋™์ „๋ถ€ํ„ฐ ๋”ํ•ด๋ณธ๋‹ค
17+
18+
// ์ด๋ฏธ ๋งŒ๋“ค์–ด๋ณธ 'ํ•ฉ' ์€ ์ง€๋‚˜๊ฐ€๋„๋ก.
19+
boolean[] visitedAmount = new boolean[amount + 1];
20+
// ํ•ฉ
21+
Queue<Integer> amountQueue = new ArrayDeque<>();
22+
23+
amountQueue.add(0); // ์‹œ์ž‘ ๊ธˆ์•ก 0
24+
visitedAmount[0] = true;
25+
26+
int coinCount = 0; // ์‚ฌ์šฉํ•œ ๋™์ „ ๊ฐœ์ˆ˜
27+
28+
while(!amountQueue.isEmpty()) {
29+
coinCount++; // ์‚ฌ์šฉํ•œ ๋™์ „ ๊ฐœ์ˆ˜ 1๊ฐœ ์ฆ๊ฐ€
30+
31+
int queueSize = amountQueue.size();
32+
33+
for(int i = 0; i < queueSize; i++) {
34+
// ํ˜„์žฌ ๊ธˆ์•ก ํ•ฉ
35+
int currentAmount = amountQueue.poll();
36+
37+
// ๋ชจ๋“  ์ฝ”์ธ์„ ๋”ํ•ด๋ณธ๋‹ค
38+
for(int coin : coins) {
39+
// ์ฝ”์ธ์„ ๋”ํ•ด ๋ณธ ํ•ฉ.
40+
int tempAmount = currentAmount + coin;
41+
42+
if (tempAmount == amount) return coinCount; // ์ตœ์†Œ ๋™์ „ ๊ฐœ์ˆ˜ ์ฐพ์Œ
43+
44+
// ์ฒ˜์Œ ๋งŒ๋“  ํ•ฉ ์ด๊ณ , ๋ชฉํ‘œ ๊ธˆ์•ก๋ณด๋‹ค ์ ์€ ํ•ฉ์ด๋ผ๋ฉด,
45+
if (tempAmount < amount && !visitedAmount[tempAmount]) {
46+
visitedAmount[tempAmount] = true;
47+
amountQueue.add(tempAmount);
48+
}
49+
}
50+
}
51+
}
52+
return -1;
53+
}
54+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* ์ตœ๋Œ€ depth ๊นŒ์ง€ ๊ฐ€๋ด์•ผ ํ•˜๋ฏ€๋กœ DFS ํƒ์ƒ‰ ์ด์šฉ
3+
*
4+
* ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ˆœํšŒํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„ O(N)
5+
* ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ ํ”„๋ ˆ์ž„ (= ํŠธ๋ฆฌ ์ตœ๋Œ€ ๋†’์ด height) ๋งŒํผ ๊ณต๊ฐ„๋ณต์žก๋„ O(H)
6+
*
7+
* Definition for a binary tree node.
8+
* public class TreeNode {
9+
* int val;
10+
* TreeNode left;
11+
* TreeNode right;
12+
* TreeNode() {}
13+
* TreeNode(int val) { this.val = val; }
14+
* TreeNode(int val, TreeNode left, TreeNode right) {
15+
* this.val = val;
16+
* this.left = left;
17+
* this.right = right;
18+
* }
19+
* }
20+
*/
21+
class Solution {
22+
public int maxDepth(TreeNode root) {
23+
if (root == null) return 0; // ์ž์‹์ด ์—†์œผ๋ฉด ์ข…๋ฃŒ
24+
25+
int left = maxDepth(root.left);
26+
int right = maxDepth(root.right);
27+
return Math.max(left, right) + 1; // ๋ฃจํŠธ๋Š” ๊ธฐ๋ณธ 1์ธต์ด๋‹ˆ๊นŒ 1 ๋”ํ•œ๋‹ค
28+
}
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* ๊ณ ์ƒํ•œ ์ด์œ : ListNode ์˜ ์‹œ์ž‘์ ์„ ๋”ฐ๋กœ ๋‘ฌ์•ผ ํ•˜๋Š” ๊ฒƒ!
3+
*
4+
* ListNode dummy = new ListNode(0); // ์‹œ์ž‘์ 
5+
* ListNode output = dummy; // ์‹œ์ž‘์  ๋’ค์— ์‹ค์ œ ์ž‘์—…์šฉ ํฌ์ธํ„ฐ
6+
*
7+
*
8+
* Definition for singly-linked list.
9+
* public class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode() {}
13+
* ListNode(int val) { this.val = val; }
14+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
15+
* }
16+
*/
17+
class Solution {
18+
public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
19+
// ๊ธธ์ด๊ฐ€ ๋‘˜๋‹ค null ์ด๋ฉด, return null
20+
if (list1 == null && list2 == null) return null;
21+
22+
ListNode dummy = new ListNode(0); // ์‹œ์ž‘์ 
23+
ListNode output = dummy; // ์‹œ์ž‘์  ๋’ค์— ์‹ค์ œ ์ž‘์—…์šฉ ํฌ์ธํ„ฐ
24+
25+
while(list1 != null && list2 != null) {
26+
27+
if (list1.val < list2.val) {
28+
output.next = list1;
29+
list1 = list1.next; // list1 ํฌ์ธํ„ฐ ์ด๋™
30+
} else {
31+
output.next = list2;
32+
list2 = list2.next; // list2 ํฌ์ธํ„ฐ ์ด๋™
33+
}
34+
output = output.next; // output ํฌ์ธํ„ฐ ์ด๋™
35+
}
36+
37+
if (list1 != null) {
38+
output.next = list1;
39+
}
40+
if (list2 != null) {
41+
output.next = list2;
42+
}
43+
44+
return dummy.next;
45+
}
46+
}

0 commit comments

Comments
ย (0)