Problem 360197 · medium · Phase 03 Linear Management & Searching

Rooms for Every Workshop

intervals · sweep line · two pointers

Every workshop is [start, end): it uses a room for the times start <= t < end. Two workshops can share a room only if they never run at the same moment; a workshop that starts exactly when another ends can reuse its room. Return the minimum number of rooms needed to host all workshops.

Examples

Input:  workshops = [[9, 12], [10, 11], [11, 14], [13, 15]]
Output: 2
Explanation: At time 10 two workshops are running ([9, 12) and [10, 11)); never three.
Input:  workshops = [[1, 4], [4, 6], [6, 9]]
Output: 1

Constraints

  • 0 <= len(workshops) <= 5 * 10**4, 0 <= start < end <= 10**9.
  • Half-open intervals: [a, b) and [b, c) can share a room.
  • Target complexity: O(n log n).

Goals

  • Sort starts and ends separately and sweep through them
  • Maintain the number of workshops running at each start time
  • Apply the half-open rule so a room frees up exactly at the end time
Starting Python…