You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event starts at startDayiand ends at endDayi, and if you attend this event, you will receive a value of valuei. You are also given an integer k which represents the maximum number of events you can attend.
You can only attend one event at a time. If you choose to attend an event, you must attend the entire event. Note that the end day is inclusive: that is, you cannot attend two events where one of them starts and the other ends on the same day.
Return the maximum sum of values that you can receive by attending events.
Example 1:
Input: events = [[1,2,4],[3,4,3],[2,3,1]], k = 2
Output: 7
Explanation: Choose the green events, 0 and 1 (0-indexed) for a total value of 4 + 3 = 7.
Example 2:
Input: events = [[1,2,4],[3,4,3],[2,3,10]], k = 2
Output: 10
Explanation: Choose event 2 for a total value of 10.
Notice that you cannot attend any other event as they overlap, and that you do not have to attend k events.
Example 3:
Input: events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3
Output: 9
Explanation: Although the events do not overlap, you can only attend 3 events. Pick the highest valued three.
Constraints:
1 <= k <= events.length
1 <= k * events.length <= 106
1 <= startDayi <= endDayi <= 109
1 <= valuei <= 106
Solutions
Solution 1: Memoization + Binary Search
Thinking
Attend at most \(k\) non-overlapping events for maximum total value. After sorting by start time, we take or skip the current event and binary-search the next feasible one.
\(\textit{dfs}(i,k)\) is the best from event \(i\) with \(k\) slots left. Skipping goes to \(i+1\); taking binary-searches the first start after this end and adds the value. Memoize the states.
First, we sort the events by their start time in ascending order. Then, we define a function \(\text{dfs}(i, k)\), which represents the maximum total value achievable by attending at most \(k\) events starting from the \(i\)-th event. The answer is \(\text{dfs}(0, k)\).
The calculation process of the function \(\text{dfs}(i, k)\) is as follows:
If we do not attend the \(i\)-th event, the maximum value is \(\text{dfs}(i + 1, k)\). If we attend the \(i\)-th event, we can use binary search to find the first event whose start time is greater than the end time of the \(i\)-th event, denoted as \(j\). Then, the maximum value is \(\text{dfs}(j, k - 1) + \text{value}[i]\). We take the maximum of the two options:
Here, \(j\) is the index of the first event whose start time is greater than the end time of the \(i\)-th event, which can be found using binary search.
Since the calculation of \(\text{dfs}(i, k)\) involves calls to \(\text{dfs}(i + 1, k)\) and \(\text{dfs}(j, k - 1)\), we can use memoization to store the computed values and avoid redundant calculations.
The time complexity is \(O(n \times \log n + n \times k)\), and the space complexity is \(O(n \times k)\), where \(n\) is the number of events.
Solution 1 sorts by start and fills a suffix table. Sorting by end instead, \(f[i][j]\) is the best using the first \(i\) events and \(j\) slots, binary-searching the last non-conflicting event.
Solution 1 sorts by start and fills a suffix table. Here we sort by end and define a prefix table.
First, sort the events, this time by end time in ascending order. Then define \(f[i][j]\) as the maximum total value by attending at most \(j\) events among the first \(i\) events. The answer is \(f[n][k]\).
For the \(i\)-th event, we can choose to attend it or not. If we do not attend, the maximum value is \(f[i][j]\). If we attend, we can use binary search to find the last event whose end time is less than the start time of the \(i\)-th event, denoted as \(h\). Then the maximum value is \(f[h + 1][j - 1] + \text{value}[i]\). We take the maximum of the two options:
Attend at most \(k\) non-overlapping events for maximum total value. \(k \times n \le 10^6\), so \(n\) itself can be \(10^6\) and listing subsets is impossible.
After sorting by start, skipping calls \(i+1\) with the same remaining slots before it returns, so the chain has length \(n\) and overflows the stack.
Later events and a smaller slot count are known if we walk backward. Let \(f[i][c]\) be the best value from event \(i\) with \(c\) slots left, and fill \(i\) from \(n-1\) down to \(0\).
First, we sort the events by their start time in ascending order. Let \(f[i][c]\) be the maximum total value achievable by attending at most \(c\) events starting from the \(i\)-th event. The answer is \(f[0][k]\), and \(f[n][c] = 0\).
Fill \(i\) from \(n - 1\) down to \(0\). If we do not attend the \(i\)-th event, the maximum value is \(f[i + 1][c]\). If \(c > 0\) and we attend it, binary search finds the first event \(j\) whose start time is greater than the end time of the \(i\)-th event. The value is then \(f[j][c - 1] + \text{value}[i]\). Take the larger of the two:
Here, \(j\) is the index of the first event whose start time is greater than the end time of the \(i\)-th event. Because \(j > i\), \(f[j]\) is already filled.
The time complexity is \(O(n \times \log n + n \times k)\), and the space complexity is \(O(n \times k)\), where \(n\) is the number of events.