Problem 352634 · easy · Phase 03 Linear Management & Searching

Attend the Most Talks

greedy · intervals · sorting by end

A conference lists talks, each [start, end). You want to attend as many complete talks as possible; you can go straight from one talk to another that starts exactly when the first ends. Return the maximum number of talks you can attend.

Examples

Input:  talks = [[1, 3], [2, 4], [3, 5], [6, 8], [5, 7]]
Output: 3
Explanation: [1, 3), [3, 5), [5, 7) fit together; no set of four does.
Input:  talks = [[1, 10], [2, 3], [4, 5]]
Output: 2

Constraints

  • 0 <= len(talks) <= 5 * 10**4, 0 <= start < end <= 10**9.
  • Half-open intervals: a talk may start at the moment the previous one ends.
  • Target complexity: O(n log n).

Goals

  • Pick the sort key that makes the greedy choice safe
  • Accept an interval only when it starts after the last accepted one ends
Starting Python…