Ctrl K

Stamp Sums in a Row

Medium
You have a set of postage stamps whose face values are listed in stamps; several stamps may share a value. An amount x can be paid if some set of stamps (each stamp used at most once) adds up to exactly x. The amount 0 can always be paid with no stamps at all. Return how many consecutive amounts starting from 0 can be paid, that is, the largest k such that every amount 0, 1, …, k - 1 is payable.
Example 1
stamps=[1, 2, 6] → 4

The sums 0, 1, 2 and 3 can be made (0 with no stamps, 3 = 1 + 2), but 4 cannot. The answer is 4.

Example 2
stamps=[3, 1, 1] → 6

Every sum from 0 to 5 can be made, for example 4 = 3 + 1 and 5 = 3 + 1 + 1. That is 6 values.

Example 3
stamps=[2] → 1

The sum 1 is out of reach, so only 0 counts.

Constraints
  • 1 ≤ len(stamps) ≤ 4 · 10⁴
  • 1 ≤ stamps[i] ≤ 4 · 10⁴
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
stamps=[1, 2, 6]
Expected
4

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