LeetCode Solutions

252. Meeting Rooms

Time: $O(n\log n)$

Space: $O(1)$

			

class Solution {
 public:
  bool canAttendMeetings(vector<vector<int>>& intervals) {
    sort(begin(intervals), end(intervals));

    for (int i = 1; i < intervals.size(); ++i)
      if (intervals[i - 1][1] > intervals[i][0])
        return false;

    return true;
  }
};
			

class Solution {
  public boolean canAttendMeetings(int[][] intervals) {
    Arrays.sort(intervals, (a, b) -> a[0] - b[0]);

    for (int i = 1; i < intervals.length; ++i)
      if (intervals[i - 1][1] > intervals[i][0])
        return false;

    return true;
  }
}
			

class Solution:
  def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
    intervals.sort()

    for i in range(1, len(intervals)):
      if intervals[i - 1][1] > intervals[i][0]:
        return False

    return True