-
Notifications
You must be signed in to change notification settings - Fork 35
/
find-conflicting-appointments.py
47 lines (44 loc) · 1.98 KB
/
find-conflicting-appointments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#Given n appointments, find all conflicting appointments
#Input: appointments = [[1,5],[3,7],[2,6],[10,15],[5,6],[4,100]]
#Output
#[3,7] conflicts with [1,5]
#[2,6] conflicts with [1,5]
#[4,100] conflicts with [1,5]
#[2,6] conflicts with [3,7]
#[5,6] conflicts with [3,7]
#[4,100] conflicts with [3,7]
#[5,6] conflicts with [2,6]
#[4,100] conflicts with [2,6]
#[4,100] conflicts with [10,15]
#[4,100] conflicts with [5,6]
#Observations:
# 1. appointments have start and end
# 2. end is greater than start
# 3. conflict happens when there is another appointment
# 4. start time conflict: when start time is between an existing appointment
# 5. end time conflict: when end time is between an existing appointment
# 6. No conflict with start time then there is no conflict with end time
#Algorithm:
# 1. Iterate through the array and check if there is conflict with current and next appointment
# 2. Next appointment is either before current appointment or after current appointment for no conflict
#Complexity:
# Time complexity: O(n^2)
# Space complexity: O(1)
#Code
appointments = [ [[1,5],[3,7],[2,6],[10,15],[5,6],[4,100]] , [[3,7],[2,6],[10,15],[5,6],[4,100]] ]
for input in appointments:
print("finding conflicts for ", input)
for index,appointment in enumerate(input):
start,end = appointment #current appointment
for next_appointment_index in range (index+1,len(input)):
cond1 = False
cond2 = False
start_next_appointment,end_next_appointment = input[next_appointment_index]
#Check if next appointment is before current appointment
if (start >= end_next_appointment):
cond1 = True
#Check if the next appointment is after current appointment
if (end <= start_next_appointment):
cond2 = True
if (cond1 == False and cond2==False):
print("[%d,%d] conflicts with [%d,%d]" % (start_next_appointment,end_next_appointment,start,end))