LeetCode 253. Meeting Rooms II

Jan. 12, 2022

Lack of built-in Priority Queues in JavaScript makes it a bit hard to do problems like this. I end looking at the solution:

/**
 * @param {number[][]} intervals
 * @return {number}
 */
 var  minMeetingRooms   = function (intervals ) {
     if  (intervals .length   <   1 )  return   0

     const  startTimes   =  []
     const  endTimes   =  []

     for  ( let  i   =   0 ; i  < intervals .length ; i  ++ ) {
        startTimes [i ]  =  intervals [i ][ 0 ]
        endTimes [i ]  =  intervals [i ][ 1 ]
    }

    startTimes .sort ((a , b ) => a  - b )
    endTimes .sort ((a , b ) => a  - b )

     let  startPtr   =   0
     let  endPtr   =   0
     let  rooms   =   0

     for  ( let  i  =  0 ; i  < intervals .length ; i  ++ ) {
         if  (startTimes [i ]  >=  endTimes [endPtr ]) {
            endPtr  ++
        }  else  {
            rooms  ++
        }
    }

     return  rooms
};