Maximize Item Counts Under Suffix Budget Queries
Hudson River TradingSoftware Engineer · HR ScreenLast updated
Determine the maximum number of items that can be purchased from a suffix of an item array within a given budget for multiple independent queries.
Problem Statement
You are given an array of positive integers prices, where prices[i] represents the price of the i-th item in a shop.
You are also given a list of queries queries, where each query is a pair [start_i, budget_i]. For each query:
- You may only purchase items at indices
jsuch thatj >= start_i. - You can choose any subset of eligible items (they do not need to be consecutive).
- Each eligible item can be purchased at most once.
- The total sum of chosen item prices cannot exceed
budget_i.
Return an array of integers representing the maximum number of items you can purchase for each query. Queries are completely independent of one another.
Function Signature
def max_items_purchased(prices: list[int], queries: list[list[int]]) -> list[int]:
pass
Input & Output Format
- Input:
prices: A list of positive integers.queries: A 2D list of integers wherequeries[k] = [start_k, budget_k].
- Output:
- A list of integers where the
k-th integer is the answer toqueries[k].
- A list of integers where the
Constraints
1 <= len(prices) <= 10001 <= len(queries) <= 10000 <= start_i < len(prices)1 <= prices[i] <= 10^91 <= budget_i <= 10^14
Examples
Example 1
Input: prices = [5, 2, 8, 1, 3], queries = [[0, 7], [2, 4], [3, 10]]
Output: [3, 2, 2]
Explanation:
- Query 0:
start = 0, budget = 7. Eligible items are[5, 2, 8, 1, 3]. Sorting by cost gives[1, 2, 3, 5, 8]. Picking items with prices1, 2, 3costs6 7). Maximum items = 3. - Query 1:
start = 2, budget = 4. Eligible items are[8, 1, 3]. Sorted:[1, 3, 8]. Picking1and3costs4 <= 4. Maximum items = 2. - Query 2:
start = 3, budget = 10. Eligible items are[1, 3]. Both items cost4 <= 10. Maximum items = 2.
Example 2
Input: prices = [10, 20, 30], queries = [[1, 5], [0, 60]]
Output: [0, 3]
Explanation:
- Query 0:
start = 1, budget = 5. The cheapest eligible item costs20, which is greater than5. Maximum items = 0. - Query 1:
start = 0, budget = 60. Sum of all items is10 + 20 + 30 = 60 <= 60. Maximum items = 3.
Hints
Hint 1
To maximize the count of purchased items without any restriction on which items to pick, greedily choose items with the lowest prices first.
Hint 2
For each query, extract the slice of prices from start_i to the end, sort it in ascending order, and count how many items can be accumulated before exceeding budget_i.
Hint 3
If multiple queries share the same start index, consider pre-sorting each suffix or caching results to avoid redundant work.
Follow-up questions
- How would you optimize your solution if queries.length increases to 10^5 while len(prices) remains at 1000?
- If both len(prices) and queries.length are up to 10^5, what data structure (e.g., persistent segment tree or merge-sort tree) could answer each query in logarithmic time?
- How does the problem change if each query specifies an arbitrary sub-array [start_i, end_i] instead of an open-ended suffix?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.