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]],
return2.

题意: 满足给定的时间段,求最少需要安排几个会议室

图解

分析

  1. new two int array named starts, ends
  2. put every start of meeting time intervals into starts array ; put every end of meeting time intervals into ends array
  3. sort two arrays in ascending [ə'sɛndɪŋ] order
  4. for loop to traverse the starts, if current start smaller than current end, we need one conference room; otherwise, if current start is bigger than current end, which means there is disjoint, we need update end
class Solution {
     public int minMeetingRooms(Interval[] intervals) {
        int[] starts = new int[intervals.length];
        int[] ends = new int[intervals.length];
        for(int i=0; i<intervals.length; i++) {
            starts[i] = intervals[i].start;
            ends[i] = intervals[i].end;
        }
        Arrays.sort(starts);
        Arrays.sort(ends);
        int rooms = 0;
        int endsItr = 0;
        for(int i=0; i<starts.length; i++) {
            if(starts[i]<ends[endsItr])
                rooms++;
            else
                endsItr++;
        }
        return rooms;
    }
}

results matching ""

    No results matching ""