Simple, Intuitive JAVA Solution


  • 0
    R

    Sorting the list according to the start times and then maintaining the end times of the rooms occupied. Checking for a new interval if it can occupy any room using endtimes.

    public class Solution {
        public int minMeetingRooms(Interval[] intervals) {
            Arrays.sort(intervals, new Comparator<Interval>() {
    
                @Override
                public int compare(Interval o1, Interval o2) {
                    return o1.start - o2.start;
                }
            });
    
            ArrayList<Integer> endTimes = new ArrayList<Integer>();
            
            for(int i = 0; i< intervals.length; i++) {
                boolean needRoom = true;
                for(int j = 0; j< endTimes.size() ; j++) {
                    if(endTimes.get(j) <= intervals[i].start) {
                        needRoom = false;
                       endTimes.set(j,intervals[i].end);
                        break;
                    }
                }
                if(needRoom) {
                     endTimes.add(intervals[i].end);
                }          
            }
    
            return endTimes.size();
        }
    }
    

Log in to reply
 

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