Dodomeki
Sign in

87. Coin Change

Medium

You are given the coin values available in moedas and a target value alvo. Return the minimum number of coins needed to make exactly the target. If it is not possible, return -1. Each coin may be used as many times as needed.

Example 1
Input: moedas = [1, 3, 4], alvo = 6
Output: 2
Explanation: Two coins of 3 form the value with the fewest pieces.
Example 2
Input: moedas = [2, 5], alvo = 3
Output: -1
Constraints
  • 1 <= moedas.length <= 20
  • 1 <= moedas[i] <= 10^4
  • 0 <= alvo <= 10^4
Dynamic programming
Caso 1
moedas =
[1,3,4]
alvo =
6
Expected =
2
Caso 2
moedas =
[2,5]
alvo =
3
Expected =
-1

Run checks these 2 cases. Submit runs all 6, including hidden ones.