Skip to content
BytePatterns

Gift Cards for an Exact Total

EasyDynamic Programming#0-1-knapsack#subset-sum~20m

Problem

You hold gift cards with the values in cards, and each card can be used at most once. A card must be used in full. Return True if some of the cards add up to exactly total, otherwise False.

Examples

Input:  cards = [3, 34, 4, 12, 5, 2], total = 9
Output: True
Why:    4 + 5 = 9
Input:  cards = [3, 34, 4, 12, 5, 2], total = 30
Output: False
Why:    the small cards add up to only 26, and the 34 card alone is already too much
Input:  cards = [7], total = 0
Output: True
Why:    edge case, using no cards pays exactly zero

Hints

0 / 3

Stuck on the idea rather than the code? 0/1 Knapsack covers it.