-1

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)

genescuba
  • 41
  • 5
  • Show us the code you have tried so far. – John Gordon Sep 30 '19 at 17:05
  • I don't see any effort from you. Show us what you have tried so far! – Klaus D. Sep 30 '19 at 17:06
  • . You could model it as a maximum flow problem to solve it in `O(N)`. Let each friend be a set of nodes and each day they can meet on another set. One unit of flow goes into each friend, and one unit of flow flows out of each day. The maximum flow of the system is your answer. – Primusa Sep 30 '19 at 18:54
  • @Primusa, Sorry I am a bit confused on how you can model this as maximum flow, could you give an example – genescuba Sep 30 '19 at 19:17

1 Answers1

0

I think a dictionary would be a good way to store this information, with the key being the day number, and the value being the number of friends. So you'd end up with a dictionary like this:

{1: 4, 2: 3, 3: 9, 4: 2}

From this dictionary we can tell that on day one, four friends are available; on day two, three friends are available; on day three, nine friends are available; and on day four, two friends are available. So, the winner is day three, since it has the largest value.

To construct this dictionary, expand each interval into its full list of days. Then for each day, if that day is not present in the dictionary, add it with a value of one, otherwise increment its current value.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • Sorry if it is not clear. We are not trying to find the day where most friends are available to meet. We can only meet 1 friend a day. We want to schedule our meetings with friends in such a way that we can meet most friends possible. See the example I gave in the original question, it should make it a bit more clear. – genescuba Sep 30 '19 at 18:06
  • Ah, I see. Is your problem that you don't know the appropriate algorithm for solving this question, or you do know it but don't know how to _implement_ it in python? – John Gordon Sep 30 '19 at 18:14
  • I am not sure how to get a more efficient algorithm than the naive one I presented above. I can implement it in python if I understand how a more efficient algorithm works on this problem – genescuba Sep 30 '19 at 18:15