P15279 [GeeksforGeeks] Number of coins1824
普及
题目描述

Given a value V and array coins[] of size M, the task is to make the change for V cents, given that you have an infinite supply of each of coins{coins_{1}, coins_{2}, ..., coins_{m}} valued coins. Find the minimum number of coins to make the change. If not possible to make change then return -1.
Example 1:
Input: V = 30, M = 3, coins[] = {25, 10, 5}
Output: 2
Explanation: Use one 25 cent coin
and one 5 cent coin
Example 2:
Input: V = 11, M = 4,coins[] = {9, 6, 5, 1}
Output: 2
Explanation: Use one 6 cent coin
and one 5 cent coin
Your Task:
You don't need to read input or print anything. Complete the function minCoins() which takes V, M and array coins as input parameters and returns the answer.
Expected Time Complexity: O(VM)
Expected Auxiliary Space: O(V)
Constraints:
1 ≤ V
M ≤ 10^{6}
All array elements are distinct

输入格式

见题面

输出格式

见题面

样例输入
V = 30, M = 3, coins[] = {25, 10, 5}
样例输出
2