Maximum Affordable Items Within Budget
Greedy & IntervalsAuto-graded · 21 test casesLast updated
Determine the maximum collection of items that can be purchased given individual item costs and a total spending limit.
You are given a non-negative integer target amount representing your available funds, alongside a list of item costs. Each entry in the cost list corresponds to a single available item.
Write a function that calculates the largest number of items you can buy without spending more than the available budget. Return the individual costs of all purchased items sorted in non-decreasing order.
If there are multiple combinations that achieve the maximum item count, choose the combination with the smallest total spending. If a tie still remains, any valid set of item prices is acceptable as long as it is returned in sorted order.
Constraints
0 <= budget <= 10^90 <= len(prices) <= 10^50 <= prices[i] <= 10^9for all0 <= i < len(prices)
Examples
Example 1
Input: max_distinct_purchases(50, [20, 10, 30, 15, 5])
Output: [5, 10, 15, 20]
Example 2
Input: max_distinct_purchases(10, [6, 4, 3, 2])
Output: [2, 3, 4]
Example 3
Input: max_distinct_purchases(5, [5, 5, 1])
Output: [1]
Hints
Hint 1
Consider how picking cheaper items affects your remaining budget compared to picking more expensive items.
Hint 2
Sorting the list of item costs beforehand makes it straightforward to process items in order of increasing cost.
Hint 3
Iterate through the sorted costs, keeping a running total until adding another item would exceed the budget.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To maximize the total number of items purchased within a fixed budget, we can use a greedy strategy:
- Sort the prices in ascending order.
- Iterate through the sorted list, purchasing the cheapest available item at each step.
- Track the cumulative expenditure (
spent). Stop as soon as adding the current item's cost would exceed the specifiedbudget. - Collect and return the prices of the purchased items.
Why Greedy Works
Suppose an optimal selection of $k$ items contains a subset of prices. If we replace any item in that selection with a cheaper (or equal-cost) item not currently selected, the total cost of the selection does not increase, and the item count remains $k$. Repeating this swap process eventually transforms any optimal subset into the $k$ cheapest items in the array. Thus, taking prices in non-decreasing order guarantees both the maximum number of items and the minimum total cost for that maximum count.
Complexity
- Time Complexity: $\mathcal{O}(N \log N)$, where $N$ is the number of items in
prices. Sorting takes $\mathcal{O}(N \log N)$ time, and the subsequent linear scan takes $\mathcal{O}(N)$ time. - Space Complexity: $\mathcal{O}(N)$ auxiliary space (or $\mathcal{O}(1)$ beyond the space required to store the sorted array / return value).
Python Reference Solution
from typing import List
def max_distinct_purchases(budget: int, prices: List[int]) -> List[int]:
chosen = []
spent = 0
for price in sorted(prices):
if spent + price > budget:
break
spent += price
chosen.append(price)
return chosen