253. Meeting Rooms II

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.

For example,
Given [[0, 30],[5, 10],[15, 20]],
return 2.

Solution:simulation

思路:
Time Complexity: O(N) Space Complexity: O(N)

Solution Code:

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
class Solution {
    public int minMeetingRooms(Interval[] intervals) {
        
        int[] start = new int[intervals.length];
        int[] end = new int[intervals.length];
        
        
        for(int i = 0; i < intervals.length; i++) {
            start[i] = intervals[i].start;
            end[i] = intervals[i].end;
        }
        
        Arrays.sort(start);
        Arrays.sort(end);
        
        int max_count = 0;
        int room_count = 0;
        int e = 0;
        for(int s = 0; s < start.length; s++) {
            room_count++;
            for(; end[e] <= start[s]; e++) {
                room_count--;
            }
            max_count = Math.max(max_count, room_count);
        }
        
        return max_count;
    }
}

你可能感兴趣的:(253. Meeting Rooms II)