I can't check out where is the bug in my program,it passes almost 60 cases.


  • 0
    4
    public int coinChange(int[] coins, int amount) {
    		if (amount == 0) {
    			return 0;
    		}
    		int MinCoin=Integer.MAX_VALUE;
    		for (int i : coins) {
    			if (i<MinCoin) {
    				MinCoin=i;
    			}
    		}
    		if (amount<MinCoin) {
    			return -1;
    		}
    		int money[]=new int[amount-MinCoin+1];
    		 money[0]=1;
    		for (int i =1; i < money.length; i++) {
    			int temp=Integer.MAX_VALUE;
    			for (int m : coins) {
    				if (i+MinCoin==m) {
    					temp=1;
    					break;
    				}
    				if (i-m>=0&&money[i-m]+1<temp) {
    					temp=money[i-m]+1;
    				}
    			}
    			if (temp!=Integer.MAX_VALUE) {
    				money[i]=temp;
    			}		
    		}	
    		if (money[amount-MinCoin]==0) {
    			return -1;
    		}
    		return money[amount-MinCoin];
    	}

Log in to reply
 

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