O(nlgn) Java Solution


  • 0
    Z
    public int minMeetingRooms(Interval[] intervals) {
        int[] arr= new int[2*intervals.length];
        HashMap<Integer,Integer> mapS = new HashMap<Integer,Integer>();
        HashMap<Integer,Integer> mapE = new HashMap<Integer,Integer>();
        int k =0;
        for(Interval i:intervals){
            mapAdd(mapS,i.start);
            mapAdd(mapE,i.end);
            arr[k++] = i.start;
            arr[k++] = i.end;
        }
        Arrays.sort(arr);
        int count =0;
        int max = 0;
        for(int x:arr){
            if(mapE.containsKey(x)){
                count--;
                mapDel(mapE,x);
                
            }else if(mapS.containsKey(x)){
                count++;
                max = Math.max(max,count);
                mapDel(mapS,x);
            }
        }
        return max;
    }
    
    public void mapAdd(HashMap<Integer,Integer> map,int x){
        if(map.containsKey(x)){
                mapS.put(x,mapS.get(x)+1);
        }else{
            mapS.put(x,1);
        }
    }
    public void mapDel(HashMap<Integer,Integer> map,int x){
        if(map.get(x) == 1){
            map.remove(x);
        }else{
            map.put(x,map.get(x)-1);
        }
    }

Log in to reply
 

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