python_动态规划/贪心算法_活动选择问题(一个问题可用动态规划算法或贪心算法求解的)
文章目录
python codesketch:pesudoCode:analyize:
python code
with recursive and iteration version:
# def activity_selector(start,finish,solved_i,scale_n):# next_i=solved_i+1# while next_i<=scale_n and start[next_i]= finish[selected_i]: # result_list = result_list+[finish[selected_i]] #iterate the result_list and the selected_i result_list = result_list+[next_i] selected_i = next_i return result_listif __name__ == "__main__": # s=[1,3,0,5,3,5,6,8,8,2,12] activity_tuples_list = [(0, 0), (1, 4), (3, 5), (0, 6), (5, 7), (3, 9), (5, 9), (6, 10), (8, 11), (8, 12), (2, 14), (12, 16)] # activity_tuples_list.sort(key=lambda tuple:tuple[1]) print(activity_tuples_list) s = [activity[0] for activity in activity_tuples_list] f = [activity[1] for activity in activity_tuples_list] print("solved by recursive:") result = activity_selector(s, f, 0, 11) print(result) print("solved by iteration:") result = greedy_activity_selector(s, f) print(result)
sketch:
pesudoCode:
analyize:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~