Simple C# code


  • 0
        var ccDic = new Dictionary<int, int>();
                int minCoins = MinCoinChange(coins, amount, ccDic);
                return minCoins == int.MaxValue ? -1 : minCoins;
            }
            private static int MinCoinChange(int[] coins, int rmAmount, Dictionary<int, int> ccDic)
            {
                if (rmAmount == 0)
                    return 0;
                if (ccDic.ContainsKey(rmAmount))
                    return ccDic[rmAmount];            
                int minCoins = int.MaxValue;
                foreach (int coin in coins)
                {
                    if (coin > rmAmount)
                        continue;
                    var minCount = MinCoinChange(coins, rmAmount - coin, ccDic);
                    minCoins = minCount < minCoins ? minCount : minCoins;
                }
                minCoins = minCoins == int.MaxValue ? minCoins : minCoins + 1;
                ccDic.Add(rmAmount, minCoins);
                return minCoins;
            }
    

Log in to reply
 

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