Problem 348277 · hard · Phase 03 Linear Management & Searching

Filling the Club's Courts

greedy · interval scheduling · binary search · exchange argument

A tennis club has courts identical courts. requests[i] = [s, e] asks for one court for the half-open time [s, e): a court freed at minute e can be booked again from minute e. The club may turn requests down. Each accepted request gets one court for its whole time, and a court holds one booking at a time. Return the largest number of requests the club can accept.

Examples

Input:  courts = 2, requests = [[1, 4], [2, 6], [3, 5], [4, 7], [6, 9]]
Output: 4
Explanation: Accept all but [2, 6]: [1, 4] then [4, 7] on one court, [3, 5] then [6, 9]
on the other. [1, 4], [2, 6] and [3, 5] all overlap at minute 3, so one must go.
Input:  courts = 1, requests = [[0, 10], [1, 2], [2, 3]]
Output: 2

Constraints

  • 1 <= courts <= 2000, 0 <= len(requests) <= 5 * 10**4, 0 <= s < e <= 10**9
  • Target complexity: about O(n log n) plus cheap list updates. Scanning every court for every request is too slow for the largest tests.

Goals

  • Generalise earliest-finish scheduling from one resource to several
  • Pick the resource whose free time fits the request most tightly
  • Keep resource free times sorted so each choice is a binary search
Starting Python…