Java solution - visit meetings one by one and add more rooms if not all meetings can fit


  • 0
    D
    public class Solution {
        public int minMeetingRooms(Interval[] intervals) {
            Arrays.sort(intervals, new Comparator<Interval>(){
                public int compare(Interval i1, Interval i2) {
                    return i1.start - i2.start;
                }
            });
            int count = 0;
            int[] visit = new int[intervals.length];
            int ret = 0;
            while (count < intervals.length) {
                int max = -1;
                ret ++;
                for (int i = 0; i < intervals.length; i++) {
                    if (visit[i] == 0) {
                        if (max > intervals[i].start) {
                            continue;
                        } else {
                            visit[i] = 1;
                            count++;
                            max = Math.max(max, intervals[i].end);
                        }
                    }
                }
            }
            return ret;
        }
    }
    

Log in to reply
 

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