File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed
src/main/java/sgyj/leetcode Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 30
30
31
31
## DP
32
32
33
- - [ 746. Min Cost Climbing Stairs] ( https://leetcode.com/problems/min-cost-climbing-stairs/description/ )
33
+ - [ 746. Min Cost Climbing Stairs] ( https://leetcode.com/problems/min-cost-climbing-stairs/description/ )
34
+ - [ 128. Longest Consecutive Sequence] ( https://leetcode.com/problems/longest-consecutive-sequence/ )
35
+ - [ 322. Coin Change] ( https://leetcode.com/problems/coin-change/description/ )
Original file line number Diff line number Diff line change
1
+ package sgyj .leetcode .yeji .section6 ;
2
+
3
+ import java .util .Arrays ;
4
+
5
+ // coin change
6
+ public class Solution322 {
7
+
8
+ public int coinChange (int [] coins , int amount ) {
9
+ int [] result = new int [amount +1 ];
10
+ Arrays .fill ( result , Integer .MAX_VALUE - 1 );
11
+ result [0 ] = 0 ;
12
+
13
+ for (int coin : coins ){
14
+ for (int n = coin ; n <=amount ; n ++){
15
+ int tmp = result [n -coin ] + 1 ;
16
+ if (result [n ]>tmp ) result [n ] = tmp ;
17
+ }
18
+ }
19
+
20
+ return result [result .length -1 ] == Integer .MAX_VALUE -1 ? -1 : result [result .length -1 ];
21
+ }
22
+
23
+ }
You can’t perform that action at this time.
0 commit comments