Golang accepted answer:


  • 0
    func coinChange(coins []int, amount int) int {
        dp:=make([]int,amount+1)
    	for i:=1;i<=amount;i++{
    		min := 2147483647
    
    		for j:=0;j<len(coins);j++{
    			if i == coins[j]{
    				min =1
    				break
    			}else{
    				if i-coins[j]<0{
    					continue
    				}else if dp[coins[j]]==-1 ||dp[i-coins[j]]==-1{
    					continue
    				}
    				temp :=dp[i-coins[j]]+dp[coins[j]]
    				if temp < min{
    					min=temp
    				}
    			}
    		}
    
    		if min==2147483647{
    		dp[i] = -1
    		}else{
    			dp[i]=min
    		}
    
    
    
    	}
    	return dp[amount]
    } 
    

Log in to reply
 

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