Skip to content

Commit 6408555

Browse files
author
shengshijun
committed
贪心算法解决选择活动问题
1 parent 9e239ad commit 6408555

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

greedy/max_task_count.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# -*- coding:UTF-8
3+
__author__ = 'shenshijun'
4+
"""
5+
贪心选择实现的最大活动个数算法
6+
"""
7+
8+
9+
def max_task_count(start_list, end_list):
10+
if len(start_list) != len(end_list):
11+
raise IndexError()
12+
# 因为Action是按照结束时间来排序的,所以第一个肯定是符合条件的
13+
task_set = [(start_list[0], end_list[0])]
14+
# 表示下一个要比较的位置
15+
for x in xrange(1, len(start_list)):
16+
if start_list[x] >= task_set[-1][1]:
17+
task_set.append((start_list[x], end_list[x]))
18+
return task_set
19+
20+
21+
def main():
22+
task_start_list = [1, 3, 0, 5, 3, 5, 6, 8, 8, 2, 12]
23+
task_end_list = [4, 5, 6, 7, 9, 9, 10, 11, 12, 14, 16]
24+
print max_task_count(task_start_list, task_end_list)
25+
26+
27+
if __name__ == "__main__":
28+
main()
29+
30+

0 commit comments

Comments
 (0)