Share my straightforward Java solution using sort


  • 0
    H
    public class Solution {
        public int minMeetingRooms(Interval[] intervals) {
            if (intervals == null || intervals.length < 2) {
                return intervals == null || intervals.length == 0 ? 0 : 1;
            }
            Arrays.sort(intervals, new Comparator<Interval>() {
                public int compare(Interval i1, Interval i2) {
                    if (i1.start == i2.start) {
                        return i1.end - i2.end;
                    } else {
                        return i1.start - i2.start;
                    }
                }
            });
            int min = 0;
            boolean[] visited = new boolean[intervals.length];
            for (int i = 0; i < intervals.length; i++) {
                if (visited[i]) {
                    continue;
                }
                min++;
                Interval pre = intervals[i];
                for (int j = i + 1; j < intervals.length; j++) {
                    if (!visited[j] && !overlap(pre, intervals[j])) {
                        visited[j] = true;
                        pre = intervals[j];
                    }
                }
                visited[i] = true;
            }
            return min;
        }
        private boolean overlap(Interval pre, Interval cur) {
            return pre.end > cur.start;
        }
    }

Log in to reply
 

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