1159. Coin Change in Minimum Steps

EasyDynamic ProgrammingUnbounded KS

Given coin denominations (unlimited supply) and a target amount, each step subtracts one coin's value from the amount. Return the minimum number of steps to reduce the amount to exactly zero, or -1 if impossible. The input is JSON {amount, coins}.

Input: JSON {amount, coins}.

Output: Integer — the minimum steps, or -1.

Examples

Example 1
Input: {"amount":11,"coins":[1,2,5]}
Output: 3
Explanation: Subtract 5, 5, 1.
Example 2
Input: {"amount":7,"coins":[3,5]}
Output: -1
Explanation: Cannot reach zero.
Example 3
Input: {"amount":0,"coins":[1]}
Output: 0
Explanation: Already zero.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →