【leetcode】322. 零钱兑换
int cmp(const void* a, const void* b){ return *(int*)b - *(int*)a; } void recursion(int* coins,int coinsSize,int amount,int cur,int sum,int cnt,int* minCnt){ if(cur==coinsSize){ if(sum==amount) *minCnt=cnt; return; } int len=(amount-sum)/coins[cur]; for (int i=len; i>=0 && cnt+i<*minCnt; i--){ if(cur==coinsSize-1 && sum+i*coins[cur]<amount) break; recursion(coins,coinsSize,amount,cur+1,sum+i*coins[cur],cnt+i,minCnt); } } int coinChange(int* coins, int coinsSize, int amount){ int minCnt=2147483647; qsort(coins,coinsSize,sizeof(int),cmp); recursion(coins,coinsSize,amount,0,0,0,&minCnt); return (minCnt==2147483647)?-1 :minCnt; }