Skip to content

Commit 3ce5b2d

Browse files
committed
rivkode coin change
1 parent 2993843 commit 3ce5b2d

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

β€Žcoin-change/rivkode.javaβ€Ž

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
1. 문제 이해
3+
μˆ«μžλ“€μ„ μ‘°ν•©ν•΄μ„œ νƒ€κ²Ÿ λ„˜λ²„λ₯Ό λ§Œμ‘±ν•˜λŠ” κ°€μž₯ μž‘μ€ μ½”μΈμ˜ 개수λ₯Ό λ°˜ν™˜
4+
λ§Œμ‘±ν•˜λŠ” 쑰합이 μ—†λ‹€λ©΄ -1 λ°˜ν™˜
5+
6+
λ°°μ—΄ μ •λ ¬ ν›„ μž‘μ€ μˆ«μžλΆ€ν„° μ‹œμž‘ν•΄μ„œ λ”ν•˜λ©° νƒ€κ²Ÿλ„˜λ²„μ™€ μΌμΉ˜ν•˜λŠ”μ§€ 체크
7+
μΌμΉ˜ν•œλ‹€λ©΄ κ·Έλ•Œμ˜ 코인 개수λ₯Ό κΈ°μ‘΄ κ°œμˆ˜μ™€ 비ꡐ 및 μ΄ˆκΈ°ν™”
8+
λ§Œμ•½ 크닀면 return ν•˜μ—¬ λ‹€μŒ 숫자둜 이동
9+
10+
2. μ˜ˆμ™Έ 상황
11+
12+
output이 12κ°€ λ˜λŠ” κ²½μš°μ™€ 0이 λ˜λŠ” κ²½μš°λŠ” 각각 무슨 경우일까 ?
13+
14+
12κ°€ λ˜λŠ” κ²½μš°λŠ” μ–΄λ–€ μ‘°ν•©μœΌλ‘œλ„ νƒ€κ²Ÿλ„˜λ²„λ₯Ό λ§Œλ“€μ§€ λͺ»ν–ˆμ„ 경우
15+
16+
0이 λ˜λŠ” κ²½μš°λŠ” amountκ°€ 0일 경우 아무 μˆ«μžλ„ λ°˜ν™˜λ˜λ©΄ μ•ˆλ  κ²½μš°μ΄λ―€λ‘œ κ°€μž₯ 처음 μ΄ˆκΈ°ν™” 되고 λ°”λ‘œ 리턴됨.
17+
18+
3. μ•Œκ³ λ¦¬μ¦˜
19+
dfs
20+
21+
4. κ΅¬ν˜„
22+
μˆ˜λ„ μ½”λ“œ
23+
dfs(μ‚¬μš©ν•œ 코인 개수, μ§€κΈˆκΉŒμ§€μ˜ κ°’)
24+
ν˜„μž¬ κ°’ == νƒ€κ²Ÿ κ°’ 체크
25+
λ§žλ‹€λ©΄ μ΅œμ†Œκ°’ μ΄ˆκΈ°ν™”
26+
λ°˜ν™˜
27+
28+
for coins
29+
μ§€κΈˆκΉŒμ§€μ˜ κ°’ + μƒˆλ‘œμš΄ 코인 <= νƒ€κ²Ÿ κ°’
30+
λ§žλ‹€λ©΄ dfs(μ‚¬μš©ν•œ 코인 개수 + 1, μ§€κΈˆκΉŒμ§€μ˜ κ°’ + μƒˆλ‘œμš΄ 코인)
31+
32+
dp
33+
34+
μž¬κ·€λ‘œ ν’€μ—ˆμœΌλ‹ˆ Time limit이 걸리지 μ•ŠκΈ° μœ„ν•΄ dpλ‘œλ„ ν’€ 수 μžˆλŠ” 점을 κ³ λ €ν•  수 μžˆλ‹€.
35+
36+
dp 점화식을 λ§Œλ“€μ–΄ 보자
37+
n 원을 λ§Œλ“€κΈ° μœ„ν•œ λ™μ „μ˜ μ΅œμ†Œ κ°œμˆ˜μ΄λ―€λ‘œ
38+
n이 λ§Œλ“€κ³  싢은 κΈˆμ•‘, coin이 ν˜„μž¬ μ„ νƒν•œ 동전이라고 ν• λ•Œ
39+
dp[n] = min(dp[n], dp[n-coin] + 1) 이 λ‚˜μ˜¨λ‹€.
40+
n-coin 이 λ‚˜μ˜€κ²Œλœ μ΄μœ λŠ” n을 λ§Œλ“€κΈ° μœ„ν•΄ νŠΉμ • μˆ«μžμ—μ„œ coin λ”ν•˜μ—¬ n 을 λ§Œλ“€μ–΄μ•Ό ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.
41+
x + coin = n -> x = n - coin 식이 λ§Œλ“€μ–΄ μ§„λ‹€.
42+
ν•΄λ‹Ή n-coin 을 λ§Œλ“€κΈ° μœ„ν•œ μ΅œμ†Œκ°’μ—μ„œ coin을 λ”ν–ˆμœΌλ―€λ‘œ 1을 λ”ν•΄μ€ŒμœΌλ‘œμ¨
43+
μ„ νƒν•œ coin μ—μ„œ n 을 λ§Œλ“€κΈ° μœ„ν•œ μ΅œμ†Œ 개수λ₯Ό 얻을 수 있기 λ•Œλ¬Έμ΄λ‹€.
44+
45+
*/
46+
47+
import java.util.*;
48+
49+
class Solution {
50+
private int min;
51+
public int coinChange(int[] coins, int amount) {
52+
// int maxLength = 12;
53+
// min = maxLength + 1;
54+
// dfs(0, 0, amount, coins);
55+
// if (min == 13) {
56+
// return -1;
57+
// }
58+
59+
int[] dp = new int[amount + 1];
60+
61+
62+
for (int i=1; i<dp.length; i++) {
63+
dp[i] = amount + 1; // μ΅œλŒ€κ°’λ³΄λ‹€ 1을 λ”ν•˜μ—¬ λ¬΄νš¨ν•œ κ°’(μ΅œλŒ€κ°’λ³΄λ‹€ 클 수 μ—†μŒ)을 μž„μ˜λ‘œ λ„£μ–΄μ€€λ‹€.
64+
}
65+
dp[0] = 0;
66+
// coin 듀을 μˆœνšŒν•˜λ©° 각 코인듀을 λ£¨ν”„λ§ˆλ‹€ ν•΄λ‹Ή λ™μ „μœΌλ‘œ μ΅œμ ν™”λ₯Ό ν•œλ‹€. κ·Έλž˜μ„œ 3번째 동전을 λŒλ•Œμ—λŠ” 이전 1, 2번째의 동전듀에 λŒ€ν•΄ λˆ„μ λœ κ²°κ³Όλ₯Ό μ–»κ²Œ λœλ‹€.
67+
for (int c : coins) {
68+
// c λΆ€ν„° μ‹œμž‘ν•˜λŠ” μ΄μœ λŠ” κ°€μ§€κ³  μžˆλŠ” 코인보닀 적은 n 을 λ§Œλ“€ 수 μ—†κΈ° λ•Œλ¬Έ
69+
for (int n = c; n < amount + 1; n++) {
70+
dp[n] = Math.min(dp[n], dp[n-c] + 1);
71+
}
72+
}
73+
74+
if (dp[amount] == amount + 1) {
75+
return -1;
76+
}
77+
78+
return dp[amount];
79+
}
80+
81+
82+
// μ‹œκ°„ 초과 λ°œμƒ
83+
// public void dfs(int count, long total, int amount, int[] coins) {
84+
// if (total == amount) {
85+
// min = Math.min(min, count);
86+
// return;
87+
// }
88+
89+
// for (int coin: coins) {
90+
// if (total + coin <= amount) {
91+
// dfs(count + 1, total + coin, amount, coins);
92+
// }
93+
// }
94+
// }
95+
}
96+

0 commit comments

Comments
Β (0)