A small country is designing its coins. Shop tills give change with a simple rule: always hand over the largest coin that still fits, then repeat on what is left. For some coin systems this rule always uses the fewest coins possible; for others it sometimes wastes coins.
Write first_bad_amount(coins, limit) that returns the smallest amount a with
1 <= a <= limit for which the till's rule uses more coins than the fewest coins that add up to
a. Return -1 if the rule is optimal for every amount up to limit. Every coin can be used any
number of times.
Examples
Input: coins = [1, 5, 6, 9], limit = 20
Output: 11
Explanation: the till pays 11 as 9 + 1 + 1 (three coins), but 5 + 6 uses only two.
Every amount from 1 to 10 is paid optimally.
Input: coins = [1, 5, 10, 25], limit = 100
Output: -1
Input: coins = [1, 12, 20], limit = 100
Output: 24
Explanation: the till pays 20 + 1 + 1 + 1 + 1 (five coins) instead of 12 + 12.
Constraints
1 <= len(coins) <= 10, the coins are distinct and1is always one of them1 <= coins[i] <= limit <= 2 * 10**4coinsis not necessarily sorted
Goals
- Simulate a greedy rule and compare it with the true optimum
- Compute the true optimum for every amount with a table
- Find the smallest input on which a greedy rule fails