Let's say I want to meet with some friends and they send me their availabilities on what days they can meet in an interval format. For example [1,4] means that a friend can meet me on days 1,2,3, and 4. I have a list of all the intervals for my friends where list[i] = interval for a friend i. A constraint I have is that I can only meet one friend a day. How do I go about writing an algorithm to find the maximum number of friends that I can meet with?
Input is a list of tuples called times
times = [(2,6), (2,20), (3,3), (5,6)] max_meetings(times) = 4
as one can meet friend1 on day2, friend2 on day4, friend3 on day 3, and friend4 on day 5.
Here is my code doing the naive solution of generating all possible n-tuples of meeting times and then just checking which fits constraints.
allPossibleSolutions = []
def helper(arr, spotsTaken, ind):
if ind == len(arr):
return
for j in range(arr[ind][0], arr[ind][1]+1):
if j not in spotsTaken:
allPossibleSolutions.append(spotsTaken+[j])
helper(arr, spotsTaken+[j], ind+1)
def max_meetings(times):
helper(times, [], 0)
myMax = 0
for sol in allPossibleSolutions:
if len(sol) > myMax:
myMax = len(sol)
return myMax
ret = max_meetings([[2, 2], [4, 5], [4, 10], [5, 6], [5, 7], [6, 6]])
# ret = 6, correct answer for this.
I am trying to find the most efficient solution, where efficiency is defined as the best time and space complexity possible. (optimizing time over space, if necessary)