Super Easy Solution, Java


  • 1
    S
    /**
     * 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; }
     * }
     */
     //[2,4], [7,10]
    public class Solution {
        public int minMeetingRooms(Interval[] intervals) {
            if(intervals == null || intervals.length == 0) {
                return 0;
            }
            
            Arrays.sort(intervals, new Comparator<Interval>() {
                @Override
                public int compare(Interval int1, Interval int2) {
                    return Integer.compare(int1.start, int2.start);
                }
            });
            
            PriorityQueue<Integer> pq = new PriorityQueue<>();
            pq.add(intervals[0].end);
            int roomRequired = 1;
            
            for(int i = 1; i<intervals.length; i++) {
                Interval cur = intervals[i];
                while(!pq.isEmpty() && cur.start >= pq.peek()) {
                    pq.poll();
                }
                pq.add(cur.end);
                roomRequired = pq.size() > roomRequired ? pq.size() : roomRequired;
            }
            
            return roomRequired;
        }
    }

Log in to reply
 

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