Cover a range with two people
An on-call rotation runs from day 1 through day 7, closed. Only one person is free all week, so you want handoffs as well. One engineer takes day 1 through some day k, and another takes day k + 1 through day 7. Which ordered pairs can do that?
The slow answer tries every pair, every handoff day and every day
Here's who is free when:
ONCALL = {
"ana": {1, 2, 3, 6, 7},
"bo": {3, 4, 5, 6, 7},
"cy": {1, 2, 3, 4},
"dev": {2, 3, 4, 5, 6, 7},
"eli": {1, 2, 3, 4, 5, 6, 7},
"fay": {1, 5, 6, 7},
}
The slow answer tries every handoff day. For a pair, a then b, the handoff day k runs from start up to end - 1, so each person gets at least one day. A k works when a is free on every day from start through k, and b on every day from k + 1 through end. Checking those days one at a time is what makes it slow.
For cy then fay, day 4 works: cy covers days 1 through 4, and fay covers 5 through 7. For ana then fay, no day works, because neither of them is free on day 4.
Here is the same calendar as a grid, with an x on each free day:
free The slow answer tries every pair, every handoff day, and every day in each part. For m engineers and a rotation of L days, that's m² pairs times L handoff days times L days checked: O(m² L²).
In the editor, a and b are the sets of days two people are free. Write handoff_days_slow(a, b, start, end): every handoff day k that works, checked day by day.