Back to questions

Coin Change Amazon Python Interview Question

Coin Change

Amazon Python Interview Question

You are given an integer array coins representing of different denominations and an integer representing a total amount of money.

Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

You may assume that you have an infinite number of each kind of coin.

Example #1

Input: coins = [2, 4, 1, 4], target_amount = 5

Output: 2

Explanation:
There are several combinations to make up the amount , such as:

  1. using five 1's (1 + 1 + 1 + 1 + 1)
  2. using three 1's and one 2 (1 + 1 + 1 + 2)
  3. using two 2's and one 1 (2 + 2 + 1)
  4. using one 4 and one 1 (4 + 1)

The combination that uses the fewest coins is the last one (using and ), which totals 2 coins.

Example #2

Input: coins = [2], target_amount = 3

Output: -1

Explanation:
It is impossible to make up the amount using only the coin , since cannot be combined in any way to reach . Therefore, the output is .

Input

Python

Output