Ctrl K

Coins to Cover Every Sum

Medium
A wallet holds coins; coins[i] is the value of the i-th coin. Each coin may be used at most once. A sum is reachable if some subset of the coins adds up to it. You may put new coins of any value into the wallet. Return the minimum number of coins you have to add so that every sum from 1 to limit inclusive becomes reachable.
Example 1
coins=[1, 3, 9], limit=14 → 2

Add a 2: with 1, 2, 3 every sum up to 6 works. Then 7 and 8 are still missing, so add a 7: with 1, 2, 3, 7, 9 everything up to 22 is reachable. Total 2.

Example 2
coins=[1, 2, 4], limit=7 → 0

1, 2 and 4 already make every sum from 1 to 7.

Example 3
coins=[5], limit=5 → 3
Constraints
  • 1 ≤ limit ≤ 10⁷
  • 1 ≤ len(coins) ≤ 10⁵
  • 1 ≤ coins[i] ≤ limit
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumLow, High, Low MediumStrictly Low, High, Low MediumFewest Meetings to Cancel Medium
loading Python…
Input
coins=[1, 3, 9], limit=14
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 112 tests on the server, hidden ones included.