user4411473
user4411473

Reputation: 199

How to calculate the intersection of multiple time ranges (best meeting time)?

Given 5 different times :

10am - 6pm
3pm - 10pm
4pm - 7pm
5pm - 1am
3pm - 12am

Is there an algorithm to calculate the best time between those given times? like the mean time?

I have tried adding up each side and divided by the number of times for that side, but the problem comes when I try to calculate the am and pm for the time, so some times would have to be weighted differently right? something like military time where 10pm = 22 / 12am = 24 / etc.

I have not coded anything for this yet, because I have been trying to figure out the bet way to go about this, but it would be programmed in JavaScript.

Edit :

 6pm - 9pm
 3pm - 8pm
 4pm - 7pm
 2pm - 10pm
 5pm - 11pm

 *6 + 3 + 4 + 2 + 5 = 20 / 5 = 4
 *9 + 8 + 7 + 10 + 11 = 45 / 5 = 9

 *best time range = 4pm - 9pm

This is an example of what I am talking about, it does not take into account the am/pm.

i want to find one time range that is best, given the five time ranges.

Upvotes: 1

Views: 1363

Answers (2)

Kimball Robinson
Kimball Robinson

Reputation: 3387

I would consider using the moment.js and moment-range.js libraries, which can quickly calculate the intersection of one or more date ranges.

https://github.com/gf3/moment-range#contains--within--overlaps--intersect--add--subtract

However, when you are trying to find the intersection of multiple time ranges, there will be cases with no solution, so you might want to find the time when most can meet (not all). Also, realistically, people might have multiple time ranges in which they are available.

I would suggest creating a sorted array of objects that contain the date-time objects, whether it's a start or end, and who it's for. Then, you could iterate through the array and add/remove the available people onto an array for each date-time. If you arrive at a time that all participants are available, you find the next end time, and that's one possible meeting time--you might have multiple solutions.

datesToCheck = [
 { date: yourDateObj, type: 'start', who: 'Juan' },
 { date: yourDateObj, type: 'end',   who: 'Juan' },
 { date: yourDateObj, type: 'start', who: 'Juan' },
 ...
]

pseudocode:

sort datesToCheck by date property;

for (i=0, i<datesToCheck.length, i++)
  date = datesToCheck[i]
  if date.type === 'start'
    availablePeopleSet.add(date.who)
  else
    availablePeopleSet.remove(date.who)
  if availablePeopleSet.size > mostAvailableFound
    mostAvailableFound = availablePeopleSet.size
    mostAvailableTimeStart = date;

I will leave it to you to figure out how to get the end date in that range (hint: it might not be the next date in the list), and how to decide which time range to choose, how to store multiple viable solutions, and handle multiple time ranges for each person.

Upvotes: 0

Ruan Mendes
Ruan Mendes

Reputation: 92284

If you want to know the mean value of all the durations, the easiest way would be first converting to 24 hours; to account for day rollover, you could add 24 hours to the end time and then get MOD the result by 24.

var timeRanges = [
    [10,18],
    [15,22],
    [16,19],
    [17,1],
    [10,18]
];

var durations = timeRanges.map(function(range){
    return ((range[1] + 24) - range[0]) % 24;
});

var totalDuration = durations.reduce(function(runningTotal, current){ 
     return runningTotal + current
}, 0);

var meanDuration = totalDuration / durations.length;

I am not sure this is what you were looking for, but that is how I interpreted your question

Upvotes: 1

Related Questions