# k is the activity which has finished and n has't finished
def greedyActivity(s, f, k, n):
m = k + 1
while (m <=n and s[m] < f[k]):
m += 1
if(m <= n):
x = [m];
x += greedyActivity(s, f, m, n)
return x
else:
return []
#in order let the first activity in the result ,we must make up
#an activity as the one activity finished before the first
s = [-2,1,3,0,5,3,5,6,8,8,2,12]
f = [-1,4,5,6,7,8,9,9,10,11,12,14,16]
k = 0
n = 11
z = greedyActivity(s,f,k,n)
print(z)
#make a for loop and every time findintg the earlist finished activity
# then change the k value
def greedyActivityAdvance(s,f,k,n):
x = [s[1]]
k = 1
for m in range(2,n+1) :
if( s[m] >= f[k]):
x.append(m)
k = m
return x
s = [-2,1,3,0,5,3,5,6,8,8,2,12]
f = [-1,4,5,6,7,8,9,9,10,11,12,14,16]
k = 0
n = 11
zz = greedyActivityAdvance(s,f,k,n)
print(zz)
5 参考文献
算法导论