Shopping Offers - Problem
Welcome to the LeetCode Store! ๐๏ธ You're shopping for multiple items, and there are some fantastic bundle deals available that could save you money.
Here's what you need to figure out: What's the minimum amount you need to pay to buy exactly the items you want?
You're given:
price[]- Regular price of each itemneeds[]- How many of each item you want to buyspecial[]- Bundle offers where each offer contains [item1_qty, item2_qty, ..., bundle_price]
Rules:
- You can use any special offer multiple times
- You cannot buy more items than you need (even if it's cheaper)
- You want to minimize the total cost
Example: If you need [2,5] items with prices [2,5] and there's a special offer [3,0,5], you could buy the bundle once and pay regular price for remaining items.
Input & Output
example_1.py โ Basic Shopping
$
Input:
price = [2,5], special = [[3,0,5],[1,2,10]], needs = [3,2]
โบ
Output:
14
๐ก Note:
We need 3 units of item 0 and 2 units of item 1. Use special offer [3,0,5] once (costs 5), then buy 2 units of item 1 individually (costs 2*5=10). Total: 5+10=15. Or use offer [1,2,10] twice (costs 20), then buy 1 unit of item 0 (costs 2). Total: 20+2=22. The minimum is 14 using the first offer once and buying remaining items individually: 5 + 2*0 + 2*5 = 15. Wait, let me recalculate: Use offer [3,0,5] for 5, then need [0,2] items, cost 0*2 + 2*5 = 10, total 15. Actually optimal is: use [1,2,10] once for 10, then need [2,0], cost 2*2 + 0*5 = 4, total 14.
example_2.py โ Individual Better
$
Input:
price = [2,3,4], special = [[1,1,0,4],[2,2,1,9]], needs = [1,2,1]
โบ
Output:
11
๐ก Note:
We need [1,2,1] items with prices [2,3,4]. Check special offers: [1,1,0,4] gives 1 of items 0&1 for 4 (saves 2+3-4=1), [2,2,1,9] gives [2,2,1] for 9 (regular cost is 2*2+2*3+1*4=14, saves 5). We can use first offer once, leaving needs [0,1,1], then buy individually: 4 + 0*2 + 1*3 + 1*4 = 11.
example_3.py โ Edge Case Empty
$
Input:
price = [2,5], special = [[3,0,5],[1,2,10]], needs = [0,0]
โบ
Output:
0
๐ก Note:
We don't need any items, so the cost is 0. No special offers are used.
Visualization
Tap to expand
Understanding the Visualization
1
Analyze Your List
Start with your needs: [3,2] items at prices [2,5] each
2
Check Bundle Deals
Available offers: [3,0,5] and [1,2,10] - which combinations work?
3
Calculate All Options
Try each valid offer combination and compare with individual purchases
4
Cache Results
Remember the best cost for each shopping state to avoid recalculation
Key Takeaway
๐ฏ Key Insight: By caching the minimum cost for each shopping state (combination of remaining needs), we avoid recalculating the same subproblems and achieve optimal performance while exploring all valid purchase combinations.
Time & Space Complexity
Time Complexity
O(k^n)
Where k is max items needed and n is number of item types, but each unique state calculated only once
โ Linear Growth
Space Complexity
O(k^n)
Space for memoization cache storing results for each unique needs state
โก Linearithmic Space
Constraints
- 1 โค n โค 6
- 0 โค price[i] โค 1000
- 1 โค len(special) โค 100
- special[i].length = n + 1
- 0 โค special[i][j] โค 50
- 0 โค needs[i] โค 102
- Each special offer must provide savings (otherwise it can be ignored)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code