Skip to the code
cf.
+10 XP

#511 Spend a Gift Card Exactly

easy

A shopper has a gift card worth total and a wishlist of items with whole-number prices. Each item can be bought at most once.

Write canSpendExactly(prices, total) that returns true if some selection of wishlist items costs exactly total, and false otherwise. Buying nothing costs 0.

Example 1

Input: prices = [3,30,4,11,5,2], total = 9
Output: true

4 + 5 = 9.

Example 2

Input: prices = [3,30,4,11,5,2], total = 29
Output: false

The 30 alone is too much, and all the other items add up to only 25.

Constraints

  • 1 <= prices.length <= 200
  • 1 <= prices[i] <= 1000
  • 0 <= total <= 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).