Skip to content

Meeting Rooms

Given an array of meeting time interval objects consisting of start and end times [[start_1,end_1],[start_2,end_2],…] (start_i < end_i), determine if a person could add all meetings to their schedule without any conflicts.

/**
* Definition of Interval:
* public class Interval {
* public int start, end;
* public Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
class Solution {
public boolean canAttendMeetings(List<Interval> intervals) {
Collections.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval obj1, Interval obj2) {
return Integer.compare(obj1.start, obj2.start);
}
});
for(int i = 0; i < intervals.size() - 1; i++) {
if(intervals.get(i).end > intervals.get(i + 1).start) {
return false;
}
}
return true;
}
}

Time complexity: O(Nlog⁡N) Space complexity: O(1)

This was my first leetcode problem in quite a few months. It was also my first time using Java in years (I decided to switch to Java away from JavaScript for Leetcode). Despite this being a relatively simple problem, I ran into a lot of trouble remembering the syntax dealing with ArrayLists in Java. Sorting ArrayLists and accessing their properties was lost on me, so I found that much more difficult than the problem itself.

In terms of the actual problem, I found it quite easy to find a solution by visualizing the meeting times, as I could then also tell what would cause an overlap.