Meeting Rooms II

Description

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

Example1

Input: intervals = [(0,30),(5,10),(15,20)]
Output: 2
Explanation:
We need two meeting rooms
room1: (0,30)
room2: (5,10),(15,20)

Example2

Input: intervals = [(2,7)]
Output: 1
Explanation: 
Only need one meeting room

思路:扫描线,建立事件。
/**
 * Definition of Interval:
 * public classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this.start = start;
 *         this.end = end;
 *     }
 * }
 */

class Event{
    int time;
    int flag;
    
    Event(int t, int s) {
        this.time = t;
        this.flag = s;
    }
}


public class Solution {
    /**
     * @param intervals: an array of meeting time intervals
     * @return: the minimum number of conference rooms required
     */
    public static Comparator<Event> comp = new Comparator<Event>() {
        public int compare(Event e1, Event e2) {
            if (e1.time == e2.time) {
                return e1.flag - e2.flag;
            }
            return e1.time - e2.time;
        }
    };
    public int minMeetingRooms(List<Interval> intervals) {
        List<Event> list = new ArrayList<>();
        for (Interval i : intervals) {
            list.add(new Event(i.start, 1));
            list.add(new Event(i.end, 0));
        }
        
        Collections.sort(list, comp);
        int count = 0, ans = 0;
        for (Event e: list) {
            if (e.flag == 1) {
                count++;
            } else {
                count --;
            }
            ans = Math.max(ans, count);
        }
        return ans;
    }
}

  



posted @ 2019-12-21 16:36  YuriFLAG  阅读(282)  评论(0)    收藏  举报