Added 638. Shopping Offers #72
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🛒 Shopping Offers (DFS + Memoization)
Problem Description
In LeetCode Store, there are n types of items.
Each item has a price, and there are several special bundle offers that let you buy multiple items together for a discounted price.
You are given:
price[i]: cost of the i-th item.
needs[i]: number of units of the i-th item you need.
special[i]: a list where:
the first n elements represent how many of each item are in the bundle,
and the last element is the bundle price.
You must buy exactly the required number of items (not more).
Return the minimum total cost to satisfy all your needs using any combination of bundles and individual purchases.
🧩 Example
Input:
price = [2,5]
special = [[3,0,5],[1,2,10]]
needs = [3,2]
Output: 14
Explanation:
Buy bundle2 (1A + 2B) for $10.
Then buy 2A individually (2 × $2 = $4).
Total = $14.
Approach
🔹 Search Space Definition:
We need to decide how many times to use each special offer to minimize cost.
Each offer can be used any number of times,
as long as it doesn’t exceed our required needs.
🔹 DFS + Memoization:
We define a recursive function:
dfs(currentNeeds) → minimum cost to fulfill these needs
For each call:
Calculate the base cost = buying all items at regular prices.
Try using each special offer:
Check if it’s valid (doesn’t exceed remaining needs).
Subtract the items from needs.
Recursively compute the cost for newNeeds.
Take the minimum cost of all possibilities.
We memoize each needs configuration to avoid recalculating overlapping states.
🔹 Intuition:
This is a search + pruning problem:
At every step, we explore all valid offers that reduce the current needs.
Since the same “needs” state may occur multiple times in different orders,
we cache (memoize) previously computed results for efficiency.
The recursion tree explores all possible combinations,
but memoization ensures each state is processed only once.
⏱️ Complexity:
Time Complexity: O(n * product(needs[i]))
(since each unique combination of remaining needs is visited once)
Space Complexity: O(product(needs[i]))
(for recursion stack + memoization map)
✅ Solution in Code (C++)