A customer has gift cards with balances in cards. Several cards may have the same balance, and each card can be used at most once.
Write giftCardSets(cards, target) that returns every distinct set of balances that adds up to exactly target. Write each set sorted ascending; the sets may be listed in any order.
Two sets are the same if they use the same balances the same number of times, even if they came from different cards. Return [] if no set works.
Input: cards = [2,5,2,1,2], target = 5 Output: [[1,2,2],[5]]
1 + 2 + 2 = 5 and 5 alone. Which 2 cards are used doesn't matter.
Input: cards = [3,3,3], target = 6 Output: [[3,3]]
Any two of the three 3 cards give the same set.
1 <= cards.length <= 201 <= cards[i] <= 501 <= target <= 50Run 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 #304 Single-Use Gift Cards. 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.