+-PriorityQueue Solution. Super easy to understand


  • 0
    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; }
     * }
     */
    public class Solution {
        public int minMeetingRooms(Interval[] intervals) {
            Queue<Integer> time = new PriorityQueue<Integer>((Integer a, Integer b) -> {
                if (Math.abs(a) != Math.abs(b))
                    return Math.abs(a) - Math.abs(b);
                else
                {
                    if (a < 0)
                        return -1;
                    else
                        return 1;
                }
            });
            for (Interval iter : intervals)
            {
                time.offer(iter.start);
                time.offer(-iter.end);
            }
            int ans = 0;
            int count = 0;
            while (!time.isEmpty())
            {
                int cur = time.poll();
                if (cur > 0)
                    count++;
                else
                    count--;
                ans = Math.max(count, ans);
                
            }
            return ans;
        }
    }
    

Log in to reply
 

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