Fewest Points to Cover a Microdollar Threshold
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Tests greedy selection when items have uniform cost but different values under capacity constraints.
You are given a collection of reward programs. Each program i offers up to balance[i] points, and every point from that program is worth exactly value[i] microdollars (integer units).
Your task is to determine the smallest total number of points you need to redeem across all programs such that their combined microdollar value is at least target. Points cannot be split, and you cannot exceed any program's balance. If it is impossible to reach the target with the available points, return -1.
You may exceed the target value; the goal is to minimize the total point count.
Constraints
1 ≤ len(balance) ≤ 1000 ≤ balance[i] ≤ 10^9for alli1 ≤ value[i] ≤ 10^9for alli0 ≤ target ≤ 10^15- All integers fit within standard 64-bit ranges, and intermediate products remain exact.
Examples
Example 1
Input: fewest_points_for_target(168000000, [36000, 12000, 25000], [12000, 9000, 8500])
Output: 14000
Hints
Hint 1
Each point costs the same to redeem: one point. They differ only in how much value they deliver. What strategy minimizes the count?
Hint 2
Sort programs by the microdollar value per point in descending order, then greedily take from the most valuable program first.
Hint 3
Use ceiling division to compute how many points you need from a program in one step, rather than iterating point by point.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each point you redeem adds exactly one to your total count, regardless of which program it comes from. Programs differ only in how much microdollar value each point provides. To minimize the number of points, you should always redeem the highest-value points first.
Greedy strategy:
- Pair each program's per-point value with its balance and sort in descending order by value.
- Walk through the sorted list. For each program, compute how many of its points are required to cover the remaining target using ceiling division:
ceil(remaining / point_value). - Redeem the minimum of that computed need and the program's available balance.
- Subtract the microdollar contribution from the remaining target.
- Stop as soon as the remaining target drops to zero or below. If you exhaust all programs and the target is still positive, return
-1.
Why it works:
Consider any fixed count k. The maximum microdollar value you can achieve with exactly k points is obtained by choosing the k individually most valuable points available. Therefore, the smallest k that covers the target is exactly the count you get by greedily taking the highest-value points until the target is met.
This is an exchange argument: if you have a valid selection that uses a lower-value point while a higher-value point remains unused, you can swap them without increasing the count or decreasing the total value. Repeating this process yields the greedy selection.
Bulk computation:
Within one program, all points have identical value, so you can compute the required count from that program in one step using ceiling division (remaining + point_value - 1) // point_value. This avoids iterating point by point, which would be too slow when balances reach millions or billions.
Complexity
- Time: O(n log n) to sort the programs, where n ≤ 100. The subsequent scan is O(n). The total time does not depend on the size of balances or the target.
- Space: O(n) for the sorted list of (value, balance) pairs.
Solution
from typing import List
def fewest_points_for_target(target: int, balance: List[int], value: List[int]) -> int:
if target <= 0:
return 0
programs = sorted(zip(value, balance), reverse=True)
points_used = 0
remaining = target
for point_value, available in programs:
if available <= 0:
continue
needed = (remaining + point_value - 1) // point_value
take = min(available, needed)
points_used += take
remaining -= take * point_value
if remaining <= 0:
return points_used
return -1