---
# System prepended metadata

title: '20191127'

---

# 20191127


Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.

Example 1:

0 --------------------- 30
    5----10
              15---20
Input: [[0, 30],[5, 10],[15, 20]]
Output: 2
"Sort"
                    x 
start = [0, 5, 15]
end = [10, 20, 30]
           x

int room = 2;

0 ---------------------------- 15
    2----------------7
       3--4 
            5----------8
Input: [[0, 15], [5, 8], [2, 7], [3, 4]]
                      x
start = [0, 2, 3, 5]
end = [4, 7, 8, 15]
          x

room++;
room++;
room++;
return 3;


Example 2:

Input: [[7,10],[2,4]]
Output: 1

```
public int minMeetingRooms(int[][] intervals) {
    if (intervals == null || intervals.length == 0) return 0;
    if (intervals.length == 1) return 1;
    
    int n = intervals.length;
    int[] start = new int[n];
    int[] end = new int[n];
    for (int i = 0; i < n; i++) {
        start[i] = intervals[i][0];
        end[i] = intervals[i][1];
    }
    
    Arrays.sort(start);
    Arrays.sort(end);
    
    int room = 0;
    int endPointer = 0;
    for (int i = 0; i < n; i++) {
        if (start[i] < end[endPointer]) {
            room++;
        } else {
            endPointer++;
        }
    }
    
    return room;
}
```
```
0 --------------------- 30
    5----10
              15---20

```
Input: [[5, 10],[15, 20],[0, 30]]

 public int minMeetingRooms(int[][] intervals) {
        if (intervals == null) return 0;
        if (intervals.length == 1) return 1;
        
        Arrays.sort(intervals, (a1, b1) -> Integer.compare(a1[1], b1[1]));
        int room = 0;
        int[] newInterval = intervals[0];
        for (int[] interval: intervals) {
            if (interval[1] >= newInterval[0]) {
                room++;
                newInterval = interval;
            }
        }
        return room;
    }

```
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.

0 --------------------- 30
    5----10
              15---20
Example 1:
Input: [[0,30],[5,10],[15,20]]
Output: false

  2 --- 4
             7 --- 10
Example 2:
Input: [[7,10],[2,4]]
Output: true

    x
[0, 5, 15]
[10, 20, 30]
 x
```
```
public boolean canAttendMeetings(int[][] intervals) {
     if (intervals == null) return false;
     if (intervals.length == 1) return true;
     
     int[] start = new int[intervals.length];
     int[] end = new int[intervals.length];
     for (int i = 0; i < intervals.length; i++){
         start[i] = intervals[i][0];
         end[i] = intervals[i][1];
     }
     
     Arrays.sort(start);
     Arrays.sort(end);
     
     
//     for (int i = 0; i < start.length - 1; i++) {
//        if (start[i + 1] < end[i]) {
//            return false;
//        }
//     }
     
     for (int i = 1, j = 0; i < intervals.length; i++) {
        if (start[i] < end[j]) {
            return false;
        } else {
             j++;
        }
     }
     return true;
}
```