Maximum Number of Events That Can Be Attended

You are given an array events where events[i] = [startDay_i, endDay_i]. Event i runs on every day from startDay_i to endDay_i inclusive, and you can attend it on any one of those days. You can attend at most one event per day. Return the maximum number of events you can attend.

30:00

Maximum Number of Events That Can Be Attended
medium
Free written solution
Topics
Companies

You are given an array events where events[i] = [startDay_i, endDay_i]. Event i runs on every day from startDay_i to endDay_i inclusive, and you can attend it on any one of those days. You can attend at most one event per day. Return the maximum number of events you can attend.

Example 1:
Input: {"events":[[1,2],[2,3],[3,4]]}
Output: 3
Constraints:
  • 1≤events.length≤1051 \leq \text{events.length} \leq 10^5

  • events[i].length=2\text{events[i].length} = 2

  • 1≤startDayi≤endDayi≤1051 \leq \text{startDay}_i \leq \text{endDay}_i \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"events":[[1,2],[2,3],[3,4]]}

3 events. Each day you may attend one event that is running that day. Greedy rule: on each day, attend the open event that ends soonest.

day

-

attended

0

in heap

0

not yet open

3

events on the calendar (sorted by start)

1234[1,2][2,3][3,4]

not open yet

open, in heap

attended (solid = day used)

expired

min-heap of end days (top = attend next)

empty

Variables
VariableValue
day-
i0
heap[]
count0
DepthFunction Call
Stack empty
0/11