meeting room 2 javascript solution code example

Example: meeting room 2 javascript solution

/**
 * @param {number[][]} intervals
 * @return {number}
 */
const minMeetingRooms = (intervals) => {
  let startList = [];
  let endList = [];
  let endPos = 0;
  let rooms = 0;
  for (let i = 0; i < intervals.length; i++) {
    startList.push(intervals[i][0]);
    endList.push(intervals[i][1]);
  }
  startList.sort((a, b) => a - b);
  endList.sort((a, b) => a - b);
  for (let i = 0; i < intervals.length; i++) {
    if (startList[i] < endList[endPos]) {
      rooms++;
    } else {
      endPos++;
    }
  }
  return rooms;
};
 
// console.log(minMeetingRooms([[7, 10],[2, 4]]));
// 1