美文网首页
253. Meeting Rooms II

253. Meeting Rooms II

作者: 夜皇雪 | 来源:发表于2016-12-14 14:53 被阅读0次
    /**
     * 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; }
     * }
     */
    public class Solution {
        public int minMeetingRooms(Interval[] intervals) {
            if(intervals==null||intervals.length==0) return 0;
            Arrays.sort(intervals,(a,b)->(a.start-b.start));
            int max=0;
            PriorityQueue<Interval> queue=new PriorityQueue<>(intervals.length,(a,b)->(a.end-b.end));
            for(int i=0;i<intervals.length;i++){
                while(!queue.isEmpty()&&intervals[i].start>=queue.peek().end) queue.poll();
                queue.offer(intervals[i]);
                max=Math.max(max,queue.size());
            }
            return max;
        }
    }
    
    public 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;
        }
    }
    

    相关文章

      网友评论

          本文标题:253. Meeting Rooms II

          本文链接:https://www.haomeiwen.com/subject/xgvimttx.html