Quite Straight forward solution in Java


  • 0

    Since I did not know min heap, I used ArrayList to keep record.

    public int minMeetingRooms(Interval[] intervals) {
            List<List<Integer>> meetings = new ArrayList<List<Integer>>();
            if(intervals.length == 0) return meetings.size();
            
            Arrays.sort(intervals, new Comparator<Interval>(){
               public int compare(Interval inter1, Interval inter2){
                   return inter1.start - inter2.start;
               } 
            });
            
            List<Integer> cur = new ArrayList<Integer>();
            cur.add(0);
            meetings.add(cur);
            
            for(Interval inter : intervals){
                int start = inter.start, end = inter.end;
                boolean newroom = true;
                for(int i = 0; i < meetings.size(); i ++){
                    List<Integer> room = meetings.get(i);
                    if(room.get(room.size() - 1) <= start){
                        room.add(end);
                        newroom = false;
                        break;
                    }
                }
                if(newroom){
                    List<Integer> room = new ArrayList<Integer>();
                    room.add(end);
                    meetings.add(room);
                }
            }
            return meetings.size();
        }
    

Log in to reply
 

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