Maximum Number of Events That Can Be Attended II - Problem
Maximum Number of Events That Can Be Attended II

You are given an array of events where events[i] = [startDayi, endDayi, valuei]. Each event represents a business opportunity: the i-th 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.

Important constraints:
• You can only attend one event at a time
• If you choose to attend an event, you must attend the entire event
• The end day is inclusive: you cannot attend two events where one starts and the other ends on the same day

Goal: Return the maximum sum of values that you can receive by attending at most k events strategically.

Input & Output

example_1.py — Basic Case
$ Input: events = [[1,2,4],[3,4,3],[2,3,1]], k = 2
Output: 7
💡 Note: Choose events [1,2,4] and [3,4,3]. Total value = 4 + 3 = 7. These events don't overlap since first ends at day 2 and second starts at day 3.
example_2.py — Single Event Optimal
$ Input: events = [[1,2,4],[3,4,3],[2,3,10]], k = 2
Output: 10
💡 Note: Although k=2, it's optimal to choose only one high-value event [2,3,10] rather than two lower-value events.
example_3.py — Maximum Capacity
$ Input: events = [[1,1,1],[2,2,2],[3,3,3],[4,4,4]], k = 3
Output: 9
💡 Note: All events are non-overlapping. We can attend 3 events with maximum values: [2,2,2], [3,3,3], [4,4,4]. Total = 2+3+4 = 9.

Visualization

Tap to expand
Conference Scheduling StrategyTimeline ViewConf A (Day 1-3)Value: $4kConf B (Day 4-6)Value: $6kConf C (Day 7-8)Value: $3kDP Decision Matrix (k=2 conferences max)Eventsk=1k=2None00A44A,B610A,B,C610Binary Search OptimizationFind Latest Compatible EventO(log n) per lookupDP State TransitionO(nk) total states🎯 Key Insight: Sort by End Time + DP with Binary SearchTime: O(n log n + nk) | Space: O(nk)✅ Optimal Solution: Maximum value = $10k with conferences A and B
Understanding the Visualization
1
Sort by End Time
Arrange all conferences by when they end - this helps us make optimal decisions
2
Find Compatible Events
For each conference, find the latest previous conference that doesn't overlap
3
Make Optimal Choice
Decide: attend this conference + best previous combination, or skip it entirely
4
Build Solution
Use DP to combine choices and find the maximum value achievable with k conferences
Key Takeaway
🎯 Key Insight: By sorting events by end time and using DP with binary search, we can efficiently explore all possible combinations while avoiding the exponential time complexity of brute force approaches.

Time & Space Complexity

Time Complexity
⏱️
O(n log n + nk)

O(n log n) for sorting, O(n log n) for binary search preprocessing, O(nk) for DP computation

n
2n
Linearithmic
Space Complexity
O(nk)

DP table of size n×k to store optimal values for each subproblem

n
2n
Linearithmic Space

Constraints

  • 1 ≤ events.length ≤ 106
  • events[i].length == 3
  • 1 ≤ startDayi ≤ endDayi ≤ 109
  • 1 ≤ valuei ≤ 106
  • 1 ≤ k ≤ events.length
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
42.8K Views
High Frequency
~25 min Avg. Time
1.4K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen