You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event starts at startDayi and 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.length1 <= k * events.length <= 1061 <= startDayi <= endDayi <= 1091 <= valuei <= 106To tackle the problem efficiently, we can use a variant of the Dynamic Programming approach combined with Binary Search. The idea is to sort events by their end times, which helps in handling overlapping issues. Once sorted, for each event, we compute two scenarios: attending the event and not attending. We also use binary search to identify the last non-overlapping event when including the current one. We arrange these scenarios into a DP table where dp[i][j] represents the maximum value we can obtain by considering the first i events with at most j events attended.
The binary search facilitates a quick lookup of the previous event that can be attended before the current one without overlap. The overall complexity is reduced by leveraging efficient lookup using binary search in a sorted list of events' end times.
We start by sorting the events by their end times. Using a dynamic programming table with dimensions [eventsSize+1][k+1], we track the maximum values possible when attending up to k events. Binary search helps identify the latest non-compatible event to incorporate the new event without overlap.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n * k * log n), where n is the number of events. The log n factor arises from binary searching over sorted events.
Space Complexity: O(n * k) for the DP table.
Another alternative approach is to make use of Greedy reasoning combined with a Sweep-Line technique. By first sorting the events based on both start and end times, we can iterate over the events to select the maximum value events that don't overlap. When we encounter a new potential starting event that fits, we check and decide to attend based on maximizing the cumulative event value without exceeding k events.
The Python solution leverages both greedy choice and a min-heap to keep track of current events such that the events do not overlap. While introducing events from the heap, it tests viability considering the constraints, particularly the start and end days.
C++
Time Complexity: O(n log n) due to sorting and heap operations.
Space Complexity: O(n) for storing events in the heap.
| Approach | Complexity |
|---|---|
| Dynamic Programming with Binary Search | Time Complexity: O(n * k * log n), where n is the number of events. The log n factor arises from binary searching over sorted events. |
| Greedy with Segment Sweeping | Time Complexity: O(n log n) due to sorting and heap operations. |
Leetcode 1353. Maximum Number of Events That Can Be Attended • Fraz • 32,349 views views
Watch 9 more video solutions →Practice Maximum Number of Events That Can Be Attended II with our built-in code editor and test cases.
Practice on FleetCode