FastPrepMaximum Number of Events That Can Be Attended

Maximum Number of Events That Can Be Attended

ByteDance logoByteDance● MediumNEW GRADPHONE SCREEN

Problem statement

You are given events, where events[i] = [startDay, endDay] means event i can be attended on any one whole day from startDay to endDay, with both days included.

  • Attending an event takes one whole day, so you can attend at most one event on any day.
  • An event needs only one of its days. You do not have to attend it on every day of its range.
  • Return the largest number of events you can attend.

Function

maxEvents(events: int[][]) → int

Examples

Example 1

events = [[2,3],[1,2],[1,2],[3,3]]return = 3

Four events fit on days 1, 2 and 3 only, so at most three can be attended. One way: [1,2] on day 1, the other [1,2] on day 2, and [3,3] on day 3. The event [2,3] is left out, so the answer is 3.

Example 2

events = [[5,5],[1,10],[5,5]]return = 2

The two [5,5] events both need day 5, and only one event fits on a day, so one of them must be skipped. The other [5,5] goes on day 5 and [1,10] goes on any other day, such as day 1. The answer is 2.

Example 3

events = [[1,4],[4,4],[2,2],[3,4]]return = 4

Put [2,2] on day 2 and [4,4] on day 4. Then [3,4] takes day 3 and [1,4] takes day 1. All four events are attended.

Constraints

  • 1 <= events.length <= 2000
  • events[i].length == 2
  • 1 <= startDay <= endDay <= 10^5

More ByteDance problems

See ByteDance hiring insights
Saved
public int maxEvents(int[][] events) {
    // Write your code here.
}
Checking account…