forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivity_Selection.py
53 lines (40 loc) · 1.38 KB
/
Activity_Selection.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
48
49
50
51
52
53
'''
Activity Selection Problem
Input :
6
1 3 0 5 8 5
2 4 6 7 9 9
Output :
4
'''
#Activity Class
class Activity:
#Store the start and end times of the activity
def __init__(self, start, end):
self.start = start
self.end = end
#Check if the current activity conflicts with the other activity
def conflicts(self,activity):
if self.end <= activity.start or self.start >= activity.end:
return False
return True
#Returns maximum number of activities that can be selected without collisions
def Activity_Selection(actList):
#Sort the activities according to ending time
actList.sort(key = lambda x : x.end)
selectedActivities = []
#First activity is always in the selected list of activities
selectedActivities.append(actList[0])
for i in range(1,len(actList)):
#If the new activity does not conflict with the last activity, then add it to selected activities
if not actList[i].conflicts(selectedActivities[-1]):
selectedActivities.append(actList[i])
#Return the length of the selected activities array
return len(selectedActivities)
n = int(input())
activityList = []
startTimes = list(map(int,input().strip().split()))
endTimes = list(map(int,input().strip().split()))
for i in range(n):
activityList.append(Activity(startTimes[i],endTimes[i]))
print(Activity_Selection(activityList))