-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathactivity_selection.py
executable file
·55 lines (45 loc) · 1.36 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
54
55
#!/usr/bin/python
# vim: foldlevel=0
"""
You are given n activities with their start and finish times.
Select the maximum number of activities that can be performed by a single
person, assuming that a person can only work on a single activity at a time.
http://www.geeksforgeeks.org/greedy-algorithms-set-1-activity-selection-problem/
"""
import operator
def solution1(S, E):
"""
Time complexity: O(nlog(n))
>>> solution1([1, 3, 0, 5, 8, 5], [2, 4, 6, 7, 9, 9])
[0, 1, 3, 4]
"""
res = []
activities = sorted(zip(S, E), key=operator.itemgetter(1))
res.append(0)
for i in range(1, len(activities)):
if activities[i][0] > activities[res[-1]][1]:
res.append(i)
return res
def dp(A, j, memo):
if memo[j] == -1:
curmax = 0
for i in range(j):
maxendinghere = dp(A, i, memo)
if A[i][1] < A[j][0] and maxendinghere+1 > curmax:
curmax = maxendinghere+1
memo[j] = curmax
return memo[j]
def solution2(S, E):
"""
Time complexity: O(n^2)
>>> solution2([1, 3, 0, 5, 8, 5], [2, 4, 6, 7, 9, 9])
4
"""
activities = sorted(zip(S, E), key=operator.itemgetter(1))
memo = [-1] * len(activities)
memo[0] = 1
dp(activities, len(activities)-1, memo)
return max(memo)
if __name__ == "__main__":
import doctest
doctest.testmod()