I have an array of rooms
Each room can have many reservations
Reservations have a start and end time.
I can detect if a reservation overlaps another reservation with
res1Start < res2End && res1End > res2Start === overlap
I am trying trying to take the reservations, decide which over lap which, take into account multiple reservations overlapping multiple other reservations and then stack them in a diagram. like this
my current method does not account for every scenario.
One being two reservations that overlap each other and overlap the exact same other reservations.
3 overlapping reservations looking good
4th one added that straddles the same reservations as the top one and also overlaps the top one
You can see that the 4th one gets one extra unit of margin from the top added then is necessary.
also if the new overlapping reservation should overlap without increasing the 'depth' of the row the depth is still increased
My question is what is the most efficient way to iterate through each room -> iterate through each reservation in that room -> compare it to each other reservation in that room only once and keep record of how many overlaps in each room
The result I am looking for is a variable for each reservation that tells me how tall it should be relative to its containing row (reservation that overlaps 2 others should be a third the height of the row) and a variable for each reservation telling me how much distance from the top of the row to the top of the reservation should be so that it has its own space on the row
each step of the solution should be possible with javascript
+25 rep for a scalable solution.
+50 rep for a full explanation of the techniques / patterns used to come to the solution.
Update: thanks you for the advice the 'greedy' solution works great for getting the tasks into the correct 'track' and if you are not concerned with the height of your row increasing then this method is all you would need. however in a gantt situation were there could be multiple overlaps the gantts row could become unreasonably high. To combat this I need to adjust the height of each task relative to how many other tasks it overlaps.
this can be done with the 'greedy' solution fairly easily, everytime there is a conflict decrease both tasks heights. the scenario that this does not cover is the following
All the tasks are placed correctly but the task on the far left is not aware that the task it overlaps overlaps with a number of other tasks so it does not decrease its height enough. the task on the top right gets placed without knowing there is an overlapping task in 'track' 2 below it so it misses out on one height decrease and visually overlaps the task below it.
Is there a way to make each task aware of each of its overlaps' overlaps without a second pass over the data?