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 item
  • needs[] - How many of each item you want to buy
  • special[] - 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
Smart Shopping OptimizationShopping ListItem 1: need 3 (@$2 each)Item 2: need 2 (@$5 each)Regular total: $16Bundle DealsDeal 1: [3,0] for $5Save: $6-$5 = $1Deal 2: [1,2] for $10Save: $12-$10 = $2Optimization Tree[3,2]Deal1Deal2[0,2]โ†’$15[2,0]โ†’$14Memoization CacheState [3,2]: cost $14State [2,0]: cost $4State [0,2]: cost $10Optimal Solution: $14Use Deal 2 once + buy 2 individual items
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

n
2n
โœ“ Linear Growth
Space Complexity
O(k^n)

Space for memoization cache storing results for each unique needs state

n
2n
โšก 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)
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 18
38.6K Views
Medium Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen