Find minimum number of rooms required Problem statement is as follows


  Given an array of time intervals (start, end) for classroom lectures (possibly overlapping), find the minimum number of rooms required.
  
  For example, given [(30, 75), (0, 50), (60, 150)], you should return 2.


This is supposed to be an easy challenge but it was not the case for me. 

Here is my solution in JavaScript

// Given an array of time intervals (start, end) for classroom lectures (possibly overlapping),
// find the minimum number of rooms required.
// For example, given [(30, 75), (0, 50), (60, 150)], you should return 2.
exports.roomsRequired = function roomsRequired(lectureIntervals) {
    function Room() {return {busy: []};}

    let rooms = [];
    lectureIntervals.forEach(lectureInterval =&gt; {
        let roomFound = false;
        rooms.forEach(room =&gt; {
            let roomBusyInLectureHours = false;
            room.busy.forEach(reserved =&gt; {
                if (lectureInterval[0] &gt; reserved[0] &amp;&amp; lectureInterval[0] &lt; reserved[1]) roomBusyInLectureHours = true;
                if (lectureInterval[1] &gt; reserved[0] &amp;&amp; lectureInterval[1] &lt; reserved[1]) roomBusyInLectureHours = true;
                if (reserved[0] &gt; lectureInterval[0] &amp;&amp; reserved[0] &lt; lectureInterval[1]) roomBusyInLectureHours = true;
                if (reserved[1] &gt; lectureInterval[0] &amp;&amp; reserved[1] &lt; lectureInterval[1]) roomBusyInLectureHours = true;
            });
            if (!roomBusyInLectureHours) {
                room.busy.push(lectureInterval);
                roomFound = true;
            }
        });
        if (!roomFound) {
            let room = new Room();
            room.busy.push(lectureInterval);
            rooms.push(room);
        }
    });
    return rooms;
};


The only test case I have so far

let rooms = exports.roomsRequired([[30, 75], [0, 50], [60, 150]]);
for (let i = 0; i &lt; rooms.length; i++) {
    console.log(rooms[i].busy)
}


Which prints

[ [ 30, 75 ] ]
[ [ 0, 50 ], [ 60, 150 ] ]


I am aware that I am not returning the number of rooms, but that is essentially the number of rows seen, so for example in the case above it is 2 as expected. 

My question is, can this code be much shorter? I suspect I am missing something obvious being this challenge easy.

Pseudo code of my implementation would be something like this

For Each Interval:
    For Each Room in Rooms:
        For Each IntervalWithinThatRoom:
            Check If IntervalWithinThatRoom overlaps with Interval
        If No Overlaps Found
            Push Interval to Room
    If Interval Not pushed to any Room
        Create new Room
        Push Interval to Room
        Push new Room to Rooms


Edit - Unit Tests I have

expect(roomsRequired.roomsRequired([[30, 75], [0, 50], [60, 150]]).length).eq(2);
expect(roomsRequired.roomsRequired([[5, 7], [0, 9], [5, 9]]).length).eq(3);
I know that my approach may not add much value, but I would like to share it with you.

Detecting room hours overlapping with this logic is correct, but it is hard to read and understand:-

if (lectureInterval[0] &gt; reserved[0] &amp;&amp; lectureInterval[0] &lt; reserved[1]) roomBusyInLectureHours = true;
if (lectureInterval[1] &gt; reserved[0] &amp;&amp; lectureInterval[1] &lt; reserved[1]) roomBusyInLectureHours = true;
if (reserved[0] &gt; lectureInterval[0] &amp;&amp; reserved[0] &lt; lectureInterval[1]) roomBusyInLectureHours = true;
if (reserved[1] &gt; lectureInterval[0] &amp;&amp; reserved[1] &lt; lectureInterval[1]) roomBusyInLectureHours = true;


I think following the approach below will make it more readable and understandable:-

let [reservedStart, reservedEnd] = reserved, [lectureStart, lectureEnd] = lectureInterval;
let busyHours = [...new Array(reservedEnd - reservedStart)].map((v, i)=&gt; reservedStart+i);
let lectureHours = [...new Array(lectureEnd - lectureStart)].map((v, i)=&gt; lectureStart+i);
roomBusyInLectureHours = busyHours.filter(hour =&gt; lectureHours.includes(hour)).length &gt; 0;