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 <= 106When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a calendar full of events with different start times, end times, and values, and you want to pick a combination that gives you the highest total value without overlapping. The brute force approach involves exploring every single combination of events you could possibly attend to see which one pays off the most.
Here's how the algorithm would work step-by-step:
class Solution:
def maxValue(self, events: list[list[int]], maximum_events: int) -> int:
sorted_events = sorted(events, key=lambda event_item: event_item[0])
total_event_count = len(sorted_events)
def explore_schedules(current_index: int, remaining_events: int) -> int:
# If we exhausted events or limits, no more value can be added to our schedule
if current_index >= total_event_count or remaining_events == 0:
return 0
skip_current_value = explore_schedules(current_index + 1, remaining_events)
next_valid_index = current_index + 1
while next_valid_index < total_event_count:
if sorted_events[next_valid_index][0] > sorted_events[current_index][1]:
break
next_valid_index += 1
# Attend current event and search for the next compatible one to maximize value
attend_current_value = sorted_events[current_index][2] + explore_schedules(next_valid_index, remaining_events - 1)
return max(skip_current_value, attend_current_value)
return explore_schedules(0, maximum_events)To find the most valuable combination of events without overlapping, we can sort the events by their starting times and make smart choices using dynamic planning and binary search. Instead of blindly testing every possible combination, we evaluate whether to attend or skip each event while remembering the best outcomes we have already calculated.
Here's how the algorithm would work step-by-step:
import bisect
class Solution:
def maxValue(self, events: list[list[int]], max_events_allowed: int) -> int:
events.sort(key=lambda event_item: event_item[0])
total_event_count = len(events)
start_times_list = [event_item[0] for event_item in events]
memoization_table = [[-1] * (max_events_allowed + 1) for _ in range(total_event_count)]
def compute_maximum_value(current_index, remaining_events):
if current_index >= total_event_count or remaining_events == 0:
return 0
if memoization_table[current_index][remaining_events] != -1:
return memoization_table[current_index][remaining_events]
# Skipping the current event allows us to evaluate the next chronological option.
skip_current_value = compute_maximum_value(current_index + 1, remaining_events)
next_valid_index = bisect.bisect_right(start_times_list, events[current_index][1])
# Attending the current event yields value and jumps straight to the next non-overlapping event.
attend_current_value = events[current_index][2] + compute_maximum_value(next_valid_index, remaining_events - 1)
memoization_table[current_index][remaining_events] = max(skip_current_value, attend_current_value)
return memoization_table[current_index][remaining_events]
# We begin our evaluation from the very first event with our full allowed budget.
return compute_maximum_value(0, max_events_allowed)| Case | How to Handle |
|---|---|
| Maximum allowed events $k$ is greater than or equal to the total number of events | We simply cap $k$ at the total number of events or let the recursive state naturally terminate when all events are processed. |
| All events overlap with each other | The algorithm correctly evaluates each overlapping event individually and returns the maximum single event value up to $k=1$. |
| Events have identical start and end days but different values | Sorting by start day preserves all of them, and the dp state evaluates each independently to pick the highest value. |
| Extreme boundary values for start day, end day, and event values | Using standard 64-bit integers for value sums prevents arithmetic overflow during accumulation. |
| Single event provided in the input array with $k=1$ | The algorithm processes the single element and returns its value without triggering out-of-bounds errors. |
| Deep recursion stack when $k$ is large and events are numerous | Using bottom-up dynamic programming or memoized top-down recursion with binary search avoids stack overflow and redundant subproblem evaluations. |
| Binary search for the next non-overlapping event yields no valid subsequent events | The search function safely returns an index out of bounds, which terminates the branching condition for future event choices. |