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
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.
We can convert the memoization approach in Solution 1 to dynamic programming.
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: