Easy Java Solution without Heap


  • 0
    C
    public int minMeetingRooms(Interval[] intervals) {
    
        List<RoomTime> l = new ArrayList();
    
        for(Interval i : intervals){
            l.add(new RoomTime(i.start, true));    
            l.add(new RoomTime(i.end, false));    
        }
        
        Collections.sort(l, new Comparator<RoomTime>(){
            @Override
            public int compare(RoomTime r1, RoomTime r2){
                if(r1.t == r2.t){
                    if(r1.isStart){
                        return 1;
                    }else{
                        return -1;
                    }
                }else{
                    return r1.t - r2.t;
                }
            }
        });
        
        int max = 0;
        int count = 0;
        
        //If start time increment counter, if not decrease it.
        for(RoomTime r : l){
            if(r.isStart){
                count++;
            }else{
                count--;
            }
            
            //Track the max
            max = Math.max(count, max);
        }
        
        return max;
    }
    
    public class RoomTime{
        int t;
        boolean isStart;
        
        public RoomTime(int t, boolean isStart){
            this.t = t;
            this.isStart = isStart;
        }
    }

Log in to reply
 

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