22/42
Part 5 of 8 · Two peopleLearn and do

On closed days, when neither person covers the range alone, a pair works if begins_b <= reach_a + 1

When neither person covers the range alone, reach_a is at most end - 1 and begins_b - 1 is at least start. Then the window is just (begins_b - 1, reach_a), and the whole test is one comparison: begins_b <= reach_a + 1. The second person has to be back by the day after the first one's run ends.

Building each person's runs is O(d log d) for d free days. Then each pair is O(1), so all pairs cost O(m²) instead of O(m² L²).

Here it is measured over every ordered pair on a 30-day rotation, days 15 through 44. Each engineer, drawn with random.Random(5), is free from day 1 through a random day c from 1 to 60. They're free again from a random day between c and 61 through day 60:

EngineersOrdered pairsPairs that workDay by dayreach_and_begin, then handoff_days
502,45099626.9 ms0.72 ms
1009,9004,160117.2 ms2.67 ms
20039,80017,711475.6 ms9.53 ms

The fast column includes computing every engineer's reach and begins, with the reach_and_begin you wrote.

In the editor, RB holds each person's (reach, begins) for the week from day 1 through day 7. Write working_pairs(rb, end): every ordered pair that splits the range by the one comparison, leaving out anyone who covers it alone.