Skip to the code
cf.
+10 XP

#523 Pay Postage With Unlimited Stamps

easy

An online shop prints shipping labels using stamp values from stamps, and it has an unlimited supply of each value. A label is valid only if the stamps on it add up to exactly postage.

Write canPayPostage(stamps, postage) that returns true if some combination of stamps adds up to exactly postage, and false otherwise. A postage of 0 needs no stamps, so it is true.

Example 1

Input: stamps = [4,7], postage = 15
Output: true

4 + 4 + 7 = 15.

Example 2

Input: stamps = [4,7], postage = 13
Output: false

Taking away 7 leaves 6 and taking away 4s leaves 9, 5, or 1, and none of those can be made.

Constraints

  • 1 <= stamps.length <= 20
  • 1 <= stamps[i] <= 1000
  • 0 <= postage <= 10000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).