File tree 1 file changed +26
-0
lines changed
1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ ์ ๋ฐฐ์ด coins๊ฐ ์ฃผ์ด์ก์ ๋ amount๋ฅผ ๋ง๋ค ๊ธฐ ์ํด ์ต์ํ์ ๋์ ๊ฐ์๋ฅผ ๋ฐํํ์ธ์.
3
+ ๋ง์ฝ ๋์ ์ผ๋ก amount ์กฐํฉ์ ๋ง๋ค์ด๋ผ ์ ์๋ค๋ฉด -1์ ๋ฆฌํดํ์ธ์.
4
+ */
5
+ import java .util .Arrays ;
6
+
7
+ class Solution {
8
+
9
+ // ์๊ฐ๋ณต์ก๋: O(n * amount), ๊ณต๊ฐ๋ณต์ก๋: O(amount)
10
+ public int coinChange (int [] coins , int amount ) {
11
+ int [] coinCnt = new int [amount + 1 ];
12
+ // coins[i]์ ์ต๋๊ฐ์ด 2^31 - 1 ์ด๋ฏ๋ก ์ต๋๊ฐ ์ค์
13
+ Arrays .fill (coinCnt , Integer .MAX_VALUE - 1 );
14
+ coinCnt [0 ] = 0 ;
15
+ for (int i = 0 ; i < coins .length ; i ++) {
16
+ for (int j = coins [i ]; j < amount + 1 ; j ++) {
17
+ coinCnt [j ] = Math .min (coinCnt [j ], coinCnt [j - coins [i ]] + 1 );
18
+ }
19
+ }
20
+ if (coinCnt [amount ] == Integer .MAX_VALUE - 1 ) {
21
+ return -1 ;
22
+ }
23
+ return coinCnt [amount ];
24
+ }
25
+ }
26
+
You canโt perform that action at this time.
0 commit comments