Java solution, using TreeMap


  • 0
    V
        public int minMeetingRooms(Interval[] itrs) 
        {
            if(itrs.length < 2)
                return itrs.length;
            Arrays.sort(itrs, new Comparator<Interval>(){
                @Override
                public int compare(Interval i1, Interval i2) { return i1.start - i2.start; }
            });
            TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>();
            tm.put(itrs[0].end, 0);
            int toRet = 0;
            for(int i = 1; i < itrs.length; i++)
            {
                int firstEnd = tm.firstEntry().getKey();
                if(firstEnd > itrs[i].start)
                {
                    toRet++;
                    tm.put(itrs[i].end, toRet);
                    continue;
                }
                tm.remove(firstEnd);
                tm.put(itrs[i].end, toRet);
            }
            return toRet + 1;
        }

Log in to reply
 

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