1153. Coin Change I — Minimum Coins

EasyDynamic ProgrammingUnbounded KS

Given coin denominations (unlimited supply) and an amount, return the fewest coins needed to make the amount, or -1 if it cannot be made. The input is JSON {amount, coins}.

Input: JSON {amount, coins}.

Output: Integer — the minimum number of coins, or -1.

Examples

Example 1
Input: {"amount":11,"coins":[1,2,5]}
Output: 3
Explanation: 5+5+1.
Example 2
Input: {"amount":3,"coins":[2]}
Output: -1
Explanation: Cannot form 3.
Example 3
Input: {"amount":0,"coins":[1]}
Output: 0
Explanation: Zero coins.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →