1374. Clock-Face Coverage
A lighthouse beam sweeps a circular coastline of length circumference, with positions numbered 0 to circumference - 1. Beam i lights the arc that runs clockwise from starts[i] to ends[i]. If starts[i] > ends[i] the arc wraps past position 0; if starts[i] == ends[i] the arc is empty.
Return, as a 64-bit integer, the total length of coast lit by at least one beam, counting overlapping parts once. Treat every arc as half-open, so a wrapping arc covers [s, circumference) and [0, e).
Split wrapping arcs into two pieces, sort the pieces by start, and merge in one sweep. Target O(n log n) time and O(n) space.
Example 1
- Input:
- circumference = 10, starts = [2,8], ends = [5,3]
- Output:
- 7
- Explanation:
The arc 2 to 5 covers [2,5); the arc 8 to 3 wraps into [8,10) and [0,3). Together they light [0,5) and [8,10), total 7.
Example 2
- Input:
- circumference = 12, starts = [3,3], ends = [3,9]
- Output:
- 6
- Explanation:
The first arc is empty; the second covers [3,9), length 6.
Example 3
- Input:
- circumference = 100, starts = [90], ends = [10]
- Output:
- 20
- Explanation:
One wrapping arc covers [90,100) and [0,10), length 20.
Constraints
- 1 ≤ n ≤ 105, where n is the length of
starts - starts.length == ends.length
- 1 ≤ circumference ≤ 109
- 0 ≤ starts[i], ends[i] < circumference
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Expected complexity
- Time
- O(n log n)
- Space
- O(n)