活动选择问题的 Python 程序
在本文中,我们将学习如何解决下面给出的问题陈述。
问题陈述−给我们 n 个活动及其各自的开始和结束时间。我们需要选择最多数量的单人活动,假设他一次只从事一项活动。
变量符号
N - 活动总数
S - 包含所有活动开始时间的数组
F - 包含所有活动结束时间的数组
现在让我们观察下面实现中的解决方案 −
# 贪婪算法
例如
# maximum number of activities that can be performed by a single person
def Activities(s, f ):
n = len(f)
print ("The selected activities are:")
# The first activity is always selected
i = 0
print (i,end=" ")
# For rest of the activities
for j in range(n):
# if start time is greator than or equal to that of previous activity
if s[j] >= f[i]:
print (j,end=" ")
i = j
# main
s = [1, 2, 0, 3, 2, 4]
f = [2, 5, 4, 6, 8, 8]
Activities(s, f)输出
The selected activities are: 0 1

所有变量都在本地范围内声明,其引用如上图所示。
结论
在本文中,我们了解了如何编写活动选择问题的 Python 程序
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP