709857. Maximum number of events that can be attended

Medium

Maximum Number of Events that can be Attended

Summary

The problem is about finding the maximum number of events that can be attended given a set of event times and a list of availability intervals. The key concepts involved are greedy algorithms, sorting, and heap data structures.

Detailed Explanation

The problem statement is as follows: We have a list of event times and a list of availability intervals. For each availability interval, we know the start time and end time. Our goal is to find the maximum number of events that can be attended such that no two overlapping events are attended simultaneously.

To solve this problem, we can use a greedy algorithm. First, sort all the events by their start times. Then, iterate over the sorted list of events. For each event, check if it overlaps with any previously selected event. If it does not overlap, select it and update the maximum number of events that can be attended.

Here's a step-by-step breakdown of the solution:

  1. Sort all the events by their start times.
  2. Initialize a variable max_events to 0, which will store the maximum number of events that can be attended.
  3. Iterate over the sorted list of events.
  4. For each event, check if it overlaps with any previously selected event. If it does not overlap, select it and increment max_events.
  5. Return max_events.

Time complexity: O(n log n) due to sorting Space complexity: O(1)

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.Arrays;
import java.util.Comparator;

public class MaximumEvents {
    public static int maxEvents(int[][] events) {
        Arrays.sort(events, Comparator.comparingInt(a -> a[0]));
        int max_events = 0;
        for (int[] event : events) {
            if (!overlaps(event, previousEvent)) {
                max_events++;
                previousEvent = event;
            }
        }
        return max_events;
    }

    private static int[] previousEvent = null;

    private static boolean overlaps(int[] event, int[] previousEvent) {
        return event[0] < previousEvent[1];
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.