share my straightforward solution


  • 0
    S
    public class Solution {
        public int minMeetingRooms(Interval[] intervals) {
            List<Integer> res = new ArrayList<>();
            
            if (intervals == null || intervals.length == 0) {
                return 0;
            }
            if (intervals.length == 1) {
                return 1;
            }
            Arrays.sort(intervals, new Comparator<Interval>() {
                @Override
                public int compare(Interval i1, Interval i2) {
                    return i1.start - i2.start;
                }
            });
            res.add(intervals[0].end);
            for (int i = 1; i < intervals.length; i++) {
                for (int j = 0; j < res.size(); j++) {
                    if (intervals[i].start >= res.get(j)) {
                        res.set(j, intervals[i].end);
                        break;
                    } else {
                        if (j == res.size() - 1) {
                            res.add(intervals[i].end);
                            break;
                        }                                    
                    }
                }
            }
            return res.size();
        }
    }
    

Log in to reply
 

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