Back to Dashboard
Conflicting Appointments
MediumProblem Statement
Given an array of intervals representing ‘N’ appointments, find out if a person can attend all the appointments.
Examples
Example 1:
- Input:
intervals = [[1,4], [2,5], [7,9]] - Output:
false - Explanation: The first two appointments overlap
Example 2:
- Input:
intervals = [[6,7], [2,4], [8,12]] - Output:
true - Explanation: None of the appointments overlap
Approach 1 Merge Intervals:
import java.util.*;
/*class Interval {
int start;
int end;
public Interval(int start, int end) {
this.start = start;
this.end = end;
}
};*/
class Solution {
public static boolean canAttendAllAppointments(Interval[] intervals) {
Arrays.sort(intervals, (a, b) -> a.start - b.start);
for (int i = 0; i < intervals.length - 1; i ++) {
if (intervals[i].end > intervals[i + 1].start) {
return false;
}
}
return true;
}
}