Can any one tell me which part of my code is wrong?


  • 0
    X

    I'm unable to pass the test case [431,62,88,428]. There must logic error in my code but I couldn't find it..

    public class Solution {
        public int coinChange(int[] coins, int amount) {
            if(amount < 1)
                return 0;
            return coinChange(coins, amount, new int[amount], 0);
        }
        
        private int coinChange(int[] coins, int rem, int[] count, int start) {
            if(start == coins.length && rem != 0)
                return -1;
            if(rem == 0)
                return 0;
            if(count[rem - 1]!= 0)
                return count[rem - 1];
            int minCost = Integer.MAX_VALUE;
            for(int i = 0; i * coins[start] <= rem; i++) {
                int res = coinChange(coins, rem - coins[start] * i, count, start + 1);
                if(res != -1) {
                    minCost = Math.min(res + i, minCost);
                }
            }
            if(minCost != Integer.MAX_VALUE) {
                count[rem - 1] = minCost;
            } else {
                count[rem - 1] = -1;
            }
            return count[rem - 1];
        }
    }
    

Log in to reply
 

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