Example: Meeting Room I - rFronteddu/general_wiki GitHub Wiki

Determine if a person can attend all meetings given their time intervals. The intervals may overlap, and you need to decide if there are any conflicts.

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings.

import java.util.Arrays;

public class MeetingRooms {
    public static boolean canAttendMeetings(int[][] intervals) {
        if (intervals == null || intervals.length == 0) {
            return true;
        }

        // Sort the intervals by start time
        Arrays.sort(intervals, (a, b) -> Integer.compare (a[0], b[0]));

        // Check for overlapping intervals
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] < intervals[i - 1][1]) {
                return false; // Found an overlap
            }
        }

        return true; // No overlaps found
    }
}