Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

322. Coin Change

Posted on 2020-09-21 | Edited on 2021-01-22

LeetCode

DP - Top Down

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
private int coinChangeUtil(int[] coins,int rem,int[] count){
if(rem<0) return -1;
if(rem==0) return 0;
if(count[rem-1]!=0) return count[rem-1];
int min=Integer.MAX_VALUE;
for(int coin:coins){
int res=coinChangeUtil(coins,rem-coin,count);
if(res>=0&&res<min) min=res+1;
}
count[rem-1]=min==Integer.MAX_VALUE?-1:min;
return count[rem-1];
}

public int coinChange(int[] coins, int amount) {
if(amount<1) return 0;
return coinChangeUtil(coins,amount,new int[amount]);
}
}

<1…131415…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%