O(nlogn) solution with TreeMap


  • 0
    K
    public int minMeetingRooms(Interval[] intervals) {
            
            TreeMap<Integer,Integer> intervalStartEndMap = new TreeMap<Integer,Integer>();
            for(int i=0;i<intervals.length;i++){
                Interval interval = intervals[i];
                incrementValue(intervalStartEndMap,interval.start,1);
                incrementValue(intervalStartEndMap,interval.end,-1);
            }
            int maxValue = 0;
            int currentValue = 0;
            for(Map.Entry<Integer,Integer> entry:intervalStartEndMap.entrySet()){
                currentValue += entry.getValue();
                if(currentValue > maxValue){
                    maxValue = currentValue;
                }
            }
            return maxValue;
            
        }
        
        private void incrementValue(TreeMap<Integer,Integer> intervalStartEndMap, int key,int value){
            if(intervalStartEndMap.containsKey(key)){
                    int entryExitCount = intervalStartEndMap.get(key);
                    entryExitCount += value;
                    intervalStartEndMap.put(key,entryExitCount);
                }else{
                    intervalStartEndMap.put(key,value);
                }
        }
    

Log in to reply
 

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