Daily Flat-Fee Cap on Overlapping Job Charges
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Sum per-day min(active cost, flat fee) over huge day ranges using an event sweep, with the result taken modulo 1,000,000,007.
A batch system runs n jobs. Job i is active on every calendar day from first_day[i] to last_day[i], both inclusive. On each day it is active, it adds unit_costs[i] to that day's bill.
For every day, the operator independently picks one of two ways to pay:
- Regular billing: pay the sum of
unit_costs[i]over all jobs active that day. - Flat fee: pay exactly
flat_feefor that day, regardless of which jobs are active.
The choice on one day does not affect any other day. A day with no active jobs costs 0 under regular billing.
Return the smallest possible total bill over all days, modulo 1_000_000_007.
Details
- Compare the true (unreduced) daily sum against
flat_fee. Apply the modulus only to the final accumulated total. - If there are no jobs, the answer is
0. - Day numbers may be very large, so the solution must not depend on iterating over every day in the range.
Constraints
0 <= n <= 10^5, andunit_costs,first_day,last_dayall have lengthn.0 <= unit_costs[i] <= 10^91 <= first_day[i] <= last_day[i] <= 10^90 <= flat_fee <= 10^9- Return an integer in
[0, 1_000_000_006].
Examples
Example 1
Input: min_total_billing([10, 20], [1, 2], [3, 4], 15)
Output: 55
Example 2
Input: min_total_billing([5], [10], [10], 3)
Output: 3
Example 3
Input: min_total_billing([4, 7, 2], [1, 2, 5], [5, 6, 8], 10)
Output: 57
Hints
Hint 1
The days do not interact: the cheapest total is the sum over days of the cheaper option for that single day.
Hint 2
Each job adds a constant to a contiguous block of days. Record +cost at the first day and -cost at last_day + 1, then sweep the sorted event days.
Hint 3
Between two consecutive event days the active sum is constant, so that stretch contributes (number of days) * min(active sum, flat_fee). Do not reduce the active sum modulo anything before the min.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The per-day choice is free and independent, so the best total is the sum over all days of min(active_sum(day), flat_fee), where active_sum(day) is the total unit_costs of jobs active on that day.
Day numbers can reach 10^9, so we cannot visit each day. Instead we use a line sweep:
- For each job, add
+costatfirst_dayand-costatlast_day + 1(the end is inclusive, so the removal happens one day later). Merge events on the same day into a single net change. - Sort the distinct event days.
- Walk through them while keeping a running
activesum. Between two consecutive event daysprevandday, the active sum is the same for every day in[prev, day - 1]. That stretch costs(day - prev) * min(active, flat_fee). - Add each contribution to the total modulo
1_000_000_007.
Why it is correct
- The total bill is a sum of per-day costs and no constraint couples different days, so minimizing each day separately minimizes the total.
- After applying all changes at event days up to
prev, the running sum equals exactly the sum of costs of jobs whose interval contains any day in[prev, day - 1]. A job is counted from its first day and removed right after its last day. - Gaps with no active jobs have sum
0, andmin(0, flat_fee) = 0, so they need no special handling. - Comparing against
flat_feeuses the exact active sum. Reducing it modulo first could make a large sum look small and wrongly skip the flat fee. Python integers are unbounded, so only the accumulated total is reduced.
Complexity
- Time:
O(n log n)for sorting at most2nevent days; the sweep is linear. - Space:
O(n)for the event map.
Reference solution
from collections import defaultdict
from typing import List
MOD = 1_000_000_007
def min_total_billing(unit_costs: List[int], first_day: List[int], last_day: List[int], flat_fee: int) -> int:
changes = defaultdict(int)
for cost, start, end in zip(unit_costs, first_day, last_day):
changes[start] += cost
changes[end + 1] -= cost
if not changes:
return 0
total = 0
active = 0
prev = None
for day in sorted(changes):
if prev is not None and day > prev:
total = (total + (day - prev) * min(active, flat_fee)) % MOD
active += changes[day]
prev = day
return total % MOD