Java solution, beats 92%


  • 2

    walk thru the intervals, find the biggest amount of meetings going on at the same time.

    /**
     * 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) {
            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 min = 0;
            int idxStarts = 0;
            int idxEnds = 0;
            int count = 0;
            while(idxStarts < intervals.length && idxEnds < intervals.length) {
                if(starts[idxStarts] < ends[idxEnds]) {
                    count++;
                    idxStarts++;
                } else {
                    count--;
                    idxEnds++;
                }
                
                if(count > min) {
                    min = count;
                }
            }
            
            return min;
        }
    }
    

Log in to reply
 

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