KatInterview

Fewest Points to Cover a Microdollar Threshold

Greedy & IntervalsAuto-graded · 19 test cases
MediumAuto-gradedGreedy & Intervalsgreedysortingmath

Last 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) ≤ 100
  • 0 ≤ balance[i] ≤ 10^9 for all i
  • 1 ≤ value[i] ≤ 10^9 for all i
  • 0 ≤ 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.