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.
Input: prices = [3,30,4,11,5,2], total = 9 Output: true
4 + 5 = 9.
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.
1 <= prices.length <= 2001 <= prices[i] <= 10000 <= total <= 10000Run 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).
I'm here to help with #511 Spend a Gift Card Exactly. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.