Coding problem
The fewest coins for an amount
The task
A vending machine gives change with as few coins as it can, using the coin values it holds.
Complete FewestCoins(int[] coins, int amount). Coins come in the given values, which are positive and distinct, with as many of each as you like. Return the fewest coins that add up to exactly amount (0 or more), or -1 when it cannot be done. An amount of 0 needs no coins.
Signature
public static int FewestCoins(int[] coins, int amount)
Examples
[1, 3, 4], 6→2. 3 + 3; starting with the largest coin gives 4 + 1 + 1, three coins.[4], 6→-1. Fours cannot make 6.
Limits: amount up to 10,000; up to 20 coin values.
The tests check: an amount of 0, no coin values, amounts that cannot be made, cases where taking the largest coin first is wrong, coins given in any order, and amount 10,000 in time.
Hints
Your code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public static class Solution
{
// The fewest coins that make exactly amount, or -1.
public static int FewestCoins(int[] coins, int amount)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.