836. K Best Events

A conference attendee gets a list of sessions. Session events[i] = [start, end, value] runs from day start to day end, both days included, and is worth value. The attendee can be in only one place per day, so two chosen sessions must not share any day: the next session has to start strictly after the previous one ends. A session whose end is smaller than its start was cancelled and cannot be attended.

The attendee may go to at most k sessions. Return the maximum total value achievable (0 if nothing can be attended). Aim for time O(n log n + n*k) and space O(n).

Example 1

Input:
events = [[2,5,12],[6,8,7],[4,9,20],[10,11,5]], k = 2
Output:
25
Explanation:

Taking the events on days 2-5 and 10-11 gives 17, but days 4-9 alone is 20 and combined with days 10-11 gives 25; the best is 25.

Example 2

Input:
events = [[3,6,15],[5,8,30],[7,9,10]], k = 1
Output:
30
Explanation:

With a single event allowed, the most valuable one is chosen: 30.

Example 3

Input:
events = [[6,4,99],[1,2,8],[3,3,9]], k = 5
Output:
17
Explanation:

The first event ends before it starts and is cancelled; the other two do not overlap, so the answer is 17.

Constraints

1 ≤ events.length ≤ 2 * 104

events[i] = [start, end, value] with 1 ≤ start, end, value ≤ 105

1 ≤ k ≤ 100

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 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n log n + n*k)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…