File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private Integer [] memo ;
3
+
4
+ public int helper (int [] coins , int remain ) {
5
+ if (remain < 0 ) { // 0 ์ดํ๋ก ๊ฐ๋ ๊ฒฝ์ฐ -1๋ก ์ง์
6
+ return -1 ;
7
+ }
8
+ if (remain == 0 ) { // ์ฒ์ ๊ฐ์ 0์ผ๋ก ์ค์
9
+ return 0 ;
10
+ }
11
+
12
+ if (memo [remain ] != null ) {
13
+ return memo [remain ];
14
+ }
15
+
16
+ int minNumCoin = Integer .MAX_VALUE ; // ์ธ๊ฐ ์ค์ ์ต์๊ฐ์ ๊ณ ๋ฅด๋ ๋ก์ง
17
+
18
+ for (int coin : coins ) {
19
+ int numCoin = helper (coins , remain - coin );
20
+ if (numCoin != -1 ) { // ๋๋ฌ ๊ฐ๋ฅํ ๊ฒฝ์ฐ
21
+ minNumCoin = Math .min (minNumCoin , numCoin + 1 );
22
+ }
23
+ }
24
+ if (minNumCoin < Integer .MAX_VALUE ) { // ๋๋ฌ ๊ฐ๋ฅํ ๊ฒฝ์ฐ
25
+ memo [remain ] = minNumCoin ;
26
+ } else {
27
+ memo [remain ] = -1 ;
28
+ }
29
+
30
+ return memo [remain ];
31
+ }
32
+
33
+ public int coinChange (int [] coins , int amount ) {
34
+ memo = new Integer [amount + 1 ];
35
+ return helper (coins , amount );
36
+ }
37
+ }
You canโt perform that action at this time.
0 commit comments