Shift Pay Totals Across Time-of-Day Wage Bands
Greedy & IntervalsAuto-graded · 18 test casesLast updated
Sum payouts for work sessions by intersecting each session with non-overlapping hourly wage bands, using interval overlap arithmetic.
Shift Pay Totals Across Time-of-Day Wage Bands
A gig worker is paid an hourly wage that changes through the day. The day is described by a list of wage bands, each given as [band_start, band_end, hourly_wage]. During the time range from band_start up to band_end, every hour worked pays hourly_wage.
The worker also completes several jobs, each given as [job_start, job_end]. A job is paid for every moment it overlaps a wage band, at that band's hourly wage. Any part of a job that lies in no wage band earns nothing.
Return the total pay over all jobs.
Rules
- Treat every interval as half-open,
[start, end). If a job ends exactly where a band begins (or vice versa), they share zero hours. - Jobs are independent. If two jobs overlap in time, each is still paid in full for its own duration. Do not merge them.
- Wage bands never overlap each other, but they may appear in any order and may leave gaps between them.
- If either list is empty, the total is
0. - Return the total as a number (a float or an integer value equal to the total is fine, e.g.
150or150.0).
Input
wage_bands: a list of[band_start, band_end, hourly_wage]triples of integers.jobs: a list of[job_start, job_end]pairs of integers.
Output
The total pay across all jobs.
Constraints
0 <= start < end <= 24for every band and every job.- Wage bands do not overlap one another.
- A job may lie inside a single band, span several bands, or fall completely outside all bands.
hourly_wageis a non-negative integer.
Examples
Example 1
Input: total_job_pay([[0, 10, 10], [10, 20, 20]], [[5, 15]])
Output: 150
Example 2
Input: total_job_pay([[0, 5, 10], [5, 10, 15]], [[0, 2], [8, 9]])
Output: 35
Hints
Hint 1
For one job and one wage band, how many hours do they have in common? Think in terms of the later of the two starts and the earlier of the two ends.
Hint 2
If the computed shared length is negative, the intervals do not touch at all, so clamp it to zero before multiplying by the wage.
Hint 3
Because bands never overlap, you can simply add up (shared hours * wage) over every job and band pair, with no sorting or merging needed.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A job earns money only during the time it shares with a wage band. For a job [js, je) and a band [bs, be), the shared length is
shared = min(je, be) - max(js, bs)
If this is zero or negative, the two intervals do not overlap, so we clamp it with max(0, ...). Multiply the clamped value by the band's hourly wage and add it to a running total. Repeat for every job and band pair.
Why it is correct
- Bands never overlap, so any instant of a job lies in at most one band. Summing the per-band overlaps counts each moment of the job at most once and at the right wage.
- Time not covered by any band overlaps nothing, so it contributes zero.
- With half-open intervals, a job that merely touches a band at an endpoint has a shared length of zero, so boundaries are never double counted.
- Jobs are handled independently, which matches the rule that overlapping jobs are each paid in full.
- Empty inputs make the loops do nothing, giving
0.0. - The order of the bands does not matter, since each pair is evaluated on its own.
Complexity
- Time: O(J * B), where J is the number of jobs and B is the number of wage bands.
- Space: O(1) extra.
Reference solution
from typing import List
def total_job_pay(wage_bands: List[List[int]], jobs: List[List[int]]) -> float:
total = 0
for job_start, job_end in jobs:
for band_start, band_end, wage in wage_bands:
shared = max(0, min(job_end, band_end) - max(job_start, band_start))
total += shared * wage
return float(total)