Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from heapq import merge
- def getFreeTimes(calender1, dayRange1, calender2, dayRange2, meetingLength):
- # We want to go through the calender values and note whether they are starts or end
- calenders, dayRanges = [calender1, calender2], [dayRange1, dayRange2]
- calTup = []
- for i, cal in enumerate(calenders):
- c = []
- for slot in cal:
- c.append((slot[0],'Start'))
- c.append((slot[1],'End'))
- c.append((dayRanges[i][1], 'Start'))
- calTup.append(c)
- # now we sort all these, since the two list are already sorted we can use
- # heapq.merge which is O(n)
- sortedMarks = list(merge(calTup[0], calTup[1]))
- # Iterate over sorted list, starts add to total and ends minus from total
- # When the total is at 0 both the calanders are free
- total, ans, lastEnd = 0, [], max(dayRange1[0], dayRange2[0])
- for i in range(len(sortedMarks)):
- if sortedMarks[i][1]=='Start':
- if total==0 and (sortedMarks[i][0]-lastEnd)>=meetingLength:
- ans.append([lastEnd, sortedMarks[i][0]])
- total += 1
- else:
- total -= 1
- if total==0:
- lastEnd = sortedMarks[i][0]
- return ans
- calender1 = [[9, 10.5],[12, 13],[16, 18]]
- dayRange1 = [9, 20]
- calender2 = [[10, 11.5],[12.5, 14.5],[14.5,15],[16, 17]]
- dayRange2 = [10, 18.5]
- meetingLength = .5
- expAns = [[11.5, 12], [15, 16], [18, 18.5]]
- ans = getFreeTimes(calender1, dayRange1, calender2, dayRange2, meetingLength)
- print(ans)
- print(ans==expAns)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement