Talentd
FleetCode

1353. Maximum Number of Events That Can Be Attended

Medium38.9% acceptance16 companies

You are given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi and ends at endDayi.

You can attend an event i at any day d where startTimei <= d <= endTimei. You can only attend one event at any time d.

Return the maximum number of events you can attend.

Example 1:

Input: events = [[1,2],[2,3],[3,4]]
Output: 3
Explanation: You can attend all the three events.
One way to attend them all is as shown.
Attend the first event on day 1.
Attend the second event on day 2.
Attend the third event on day 3.

Example 2:

Input: events= [[1,2],[2,3],[3,4],[1,2]]
Output: 4

Constraints:

  • 1 <= events.length <= 105
  • events[i].length == 2
  • 1 <= startDayi <= endDayi <= 105

Hints open one at a time

Hint 1
Sort the events by the start time and in case of tie by the end time in ascending order.
Hint 2
Loop over the sorted events. Attend as much as you can and keep the last day occupied. When you try to attend new event keep in mind the first day you can attend a new event in.
11:50
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maxEvents(self, events: List[List[int]]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[[1,2],[2,3],[3,4]]
Input
[[1,2],[2,3],[3,4],[1,2]]
Your code is saved on this device.Solution