"""
You are given an integer array coins representing coins of different denominations and an integer amount 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 = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Example 3:

Input: coins = [1], amount = 0
Output: 0
"""

import math


def coin_change(coins: list[int], amount: int) -> int:
    counts = [math.inf] * (amount + 1)  # the minium counts of coin for *i*.
    counts[0] = 0

    for i in range(1, amount + 1):
        indices = [i - x for x in coins if i - x >= 0]
        try:
            counts[i] = min([counts[x] for x in indices]) + 1
        except ValueError:
            pass

    return -1 if math.isinf(counts[amount]) else int(counts[amount])


if __name__ == "__main__":
    assert coin_change([1, 2, 5], 11) == 3
    assert coin_change([2], 3) == -1
    assert coin_change([1], 0) == 0
