[LeetCode 253] Meeting Rooms II

Mavs發表於2015-10-13

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:

put start and negative end into list, sort it based on the absolute value. use two variable to record minimum meeting room

public int minMeetingRooms(Interval[] intervals) {
        int res = 0;
        if(intervals.length == 0) return res;
        if(intervals.length == 1) return res+1;
        List<Integer> points = new ArrayList<>();
        for(int i=0;i<intervals.length;i++) {
            points.add(intervals[i].start);
            points.add(-intervals[i].end);
        }
        Collections.sort(points, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return Math.abs(o1) - Math.abs(o2);
            }
        });
        int local = 0;
        for(int i=0;i<points.size();i++) {
            if(points.get(i)>=0) {
                local++;
            }
            else local--;
            res = Math.max(local, res);
        }
        return res;
    }





相關文章