Concise Java AC Solution Using Sort and MinHeap


  • 0
    A
    public int minMeetingRooms(final Interval[] intervals) {
    	if (intervals.length == 0) {
    		return 0;
    	}
    
    	Arrays.sort(intervals, (interval1, interval2) -> {
    		int compare = Integer.compare(interval1.start, interval2.start);
    		if (compare != 0) {
    			return compare;
    		} else {
    			return Integer.compare(interval1.end, interval2.end);
    		}
    	});
    
    	PriorityQueue<Integer> minHeap = new PriorityQueue<>();
    	for (Interval interval : intervals) {
    		if (!minHeap.isEmpty() && minHeap.peek() <= interval.start) {
    			minHeap.poll();
    		}
    
    		minHeap.offer(interval.end);
    	}
    
    	return minHeap.size();
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.