Problem 305898 · medium · Phase 03 Linear Management & Searching

Fewest Inspections to Cover Every Shift

sorting · intervals · greedy

Each worker's shift is a closed interval [start, end]. An inspector who visits at time t observes every shift with start <= t <= end. Return the minimum number of visits needed so that every shift is observed at least once.

Examples

Input:  shifts = [[1, 4], [2, 3], [5, 7], [6, 9]]
Output: 2
Explanation: Visiting at t = 3 and t = 7 covers all four shifts.
Input:  shifts = [[1, 2], [2, 3], [3, 4]]
Output: 2
Explanation: t = 2 observes the first two shifts; a second visit is needed for [3, 4].

Constraints

  • 0 <= len(shifts) <= 10**5
  • 0 <= start <= end <= 10**9
  • Target complexity: O(n log n).

Goals

  • Sort intervals by their right endpoint
  • Place each inspection as late as possible so it covers the most upcoming intervals
  • Scan once, skipping intervals already covered
Starting Python…