Skip to content

Commit 628f53a

Browse files
author
Partho Biswas
committed
Calendar_Matching
1 parent b75d81e commit 628f53a

File tree

3 files changed

+78
-19
lines changed

3 files changed

+78
-19
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
def calendarMatching(calendar1, dailyBounds1, calendar2, dailyBounds2, meetingDuration):
2+
updatedCalender1 = updateCalender(calendar1, dailyBounds1)
3+
updatedCalender2 = updateCalender(calendar2, dailyBounds2)
4+
mergedCalender = mergeCalenders(updatedCalender1, updatedCalender2)
5+
flattenedCalender = flattenCalender(mergedCalender)
6+
return getMatchingAvailabilities(flattenedCalender, meetingDuration)
7+
8+
9+
def updateCalender(calendar, dailyBounds):
10+
updatedCal = []
11+
updatedCal.append(["0:00", dailyBounds[0]])
12+
updatedCal.extend(calendar)
13+
updatedCal.append([dailyBounds[1], "23:59"])
14+
return list(map(lambda m: [timeToMinuites(m[0]), timeToMinuites(m[1])], updatedCal))
15+
16+
17+
def timeToMinuites(time):
18+
hour, mins = list(map(int, time.split(":")))
19+
return hour * 60 + mins
20+
21+
22+
def mergeCalenders(calender1, calender2):
23+
merged = []
24+
i, j = 0, 0
25+
while i < len(calender1) and j < len(calender2):
26+
meeting1, meeting2 = calender1[i], calender2[j]
27+
if meeting1[0] < meeting2[0]:
28+
merged.append(meeting1)
29+
i += 1
30+
else:
31+
merged.append(meeting2)
32+
j += 1
33+
34+
if i < len(calender1):
35+
merged.extend(calender1[i:])
36+
if j < len(calender2):
37+
merged.extend(calender2[j:])
38+
return merged
39+
40+
41+
def flattenCalender(calender):
42+
flattened = [calender[0][:]]
43+
for i in range(1, len(calender)):
44+
currMeetingStart, currMeetingEnd = calender[i]
45+
prevMeeringStart, prevMeeringEnd = flattened[-1]
46+
if prevMeeringEnd >= currMeetingStart:
47+
newPrevMeeting = [prevMeeringStart, max(prevMeeringEnd, currMeetingEnd)]
48+
flattened[-1] = newPrevMeeting
49+
else:
50+
flattened.append(calender[i])
51+
return flattened
52+
53+
54+
def getMatchingAvailabilities(calender, meetingDuration):
55+
availabilities = []
56+
for i in range(1, len(calender)):
57+
start = calender[i - 1][1]
58+
end = calender[i][0]
59+
duration = end - start
60+
if duration >= meetingDuration:
61+
availabilities.append([start, end])
62+
return list(map(lambda m: [minuitesToTime(m[0]), minuitesToTime(m[1])], availabilities))
63+
64+
65+
def minuitesToTime(minuites):
66+
hours = minuites // 60
67+
mins = minuites % 60
68+
minsStr = "0" + str(mins) if mins < 10 else str(mins)
69+
return "{}:{}".format(str(hours), minsStr)
70+
71+
72+
73+
74+
75+
Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
1-
## Calendar Matching
1+
## Calendar_Matching
22

33
#### Problem Statement
44

55

6-
Imagine that you want to schedule a meeting of a certain duration with a coworker. You have access to your calendar and your coworker's
7-
calendar (both of which contain your respective meetings for the day, in the form of [startTime, endTime]), as well as both of your daily
8-
bounds (i.e., the earliest and latest times at which you're available for meetings every day, in the form of [earliestTime, latestTime]). Write a
9-
function that takes in your calendar, your daily bounds, your coworker's calendar, your coworker's daily bounds, and the duration of the
10-
meeting that you want to schedule, and that returns a list of all the time blocks (in the form of [startTime, endTime]) during which you could
11-
schedule the meeting. Note that times will be given and should be returned in military time (example times: '8:30', '9:01', '23:56').
12-
13-
Sample input:
14-
[['9:00', '10:30'], ['12:00', '13:00'], ['16:00', '18:00']]
15-
['9:00', '20:00']
16-
[['10:00', '11:30'], ['12:30', '14:30'], ['14:30', '15:00'], ['16:00', '17:00']]
17-
['10:00', '18:30']
18-
30
19-
20-
Sample output: [['11:30', '12:00'], ['15:00', '16:00'], ['18:00', '18:30']]
6+
![alt text](Calendar_Matching.png "Calendar_Matching")
217

228

239

2410
#### Explanation
2511

26-
We can use a Stack here
2712

2813

2914
#### Solution
3015

31-
Check this [Python](../python/Calendar_Matching.py) code.
32-
16+
Check this [Python](../python/Calendar_Matching.py) code.
331 KB
Loading

0 commit comments

Comments
 (0)