Hi umnshuati, I just imitate your code, Could you please help me to just check why it is wrong? Thanks


  • 0
    O
    public class Solution {
    public int coinChange(int[] coins, int amount) {
        if(coins == null || coins.length == 0 || amount == 0) return 0;
        int[] min = new int[1];
        min[0] = Integer.MAX_VALUE;
        Arrays.sort(coins);
        helper(coins, amount, min, 0, coins.length - 1);
        return min[0] == Integer.MAX_VALUE ? -1 : min[0];
    }
    public void helper(int[] coins, int amount, int[] min, int cur_min, int idx){
        if(idx == -1) return;
        int num = amount / coins[idx];
        for(int i = num; i >= 0; i--){
            int remain = amount - coins[idx] * i;
            cur_min +=  i;
            if(remain > 0 && cur_min< min[0]){
                helper(coins, remain, min, cur_min, idx - 1);
            }else if(cur_min< min[0]){
                min[0] = cur_min;
            }else{
                break;
            }
        }
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.