选出最大不冲突的活动列表
def activity_selection(s, f):
m = [0]
tmp = f[0]
for i in range(1, len(f)):
next_ = s[i]
if next_ >= tmp:
m.append(i)
tmp = f[i]
if i < len(f) - 1:
next_ = s[i + 1]
return m
# 测试通过
s = [1, 3, 0, 5, 3, 5, 6, 8, 8, 2, 12] # 活动开始时间
f = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] # 活动结束时间
print(activity_selection(s, f)) # 选择出来的活动索引号
贪心思想,包含了具有最早结束时间的活动的列表一定可以是最大列表,可以使用归纳法证明这一点(使用替换技术)