Maximum Ice Cream Bars
Real-world version: Flea Market Record HaulThe statement here is our own: the same inputs, outputs and technique.
A collector arrives at a flea market holding budget tokens, the only currency the stalls accept.
One stall has a crate of second-hand records laid out. Record i is tagged at prices[i] tokens. The collector may take away any group of records, in any combination, as long as the tags of the records taken add up to no more than budget. Records are sold whole: there is no haggling, no swapping and no part payment, and each record in the crate can be taken at most once even when two of them carry the same tag.
Return the largest number of records the collector can walk away with.
Examples
Example 1
- Input
- prices = [9, 4, 12, 4, 30], budget = 17
- Output
- 3
Taking the two records tagged 4 and the one tagged 9 uses 4 + 4 + 9 = 17 tokens, which is the whole budget, and hands over three records.
Example 2
- Input
- prices = [10, 3, 3, 3], budget = 11
- Output
- 3
The three records tagged 3 cost 9 tokens together, leaving 2 tokens unspent and three records in hand.
Example 3
- Input
- prices = [40, 25], budget = 12
- Output
- 0
Both tags are above the whole budget, so nothing can be taken and the collector leaves empty-handed.
Constraints
- prices.length == n
- 1 <= n <= 10^5
- 1 <= prices[i] <= 10^5
- 1 <= budget <= 10^8
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def max_records(prices: list[int], budget: int) -> int:public int maxRecords(int[] prices, int budget)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's