理解
习题
习题1
示例代码
求解dp数组
list_height = [100,90,70,80,60,65,80] #
list_dp = [1,] #
# 一个拦截系统能拦截的最大导弹数
# 求最长递减子序列
while len(list_dp) < len(list_height):
for i in range(len(list_dp)-1,-1,-1): # 逆序查找
if list_height[i] >= list_height[len(list_dp)]:
list_dp.append(list_dp[i]+1)
break
else:
list_dp.append(1)
print(list_dp)
# 拦截所有导弹所需的系统数
# 求最长递增子序列
while len(list_dp) < len(list_height):
for i in range(len(list_dp)-1,-1,-1):
if list_height[i] <= list_height[len(list_dp)]:
list_dp.append(list_dp[i]+1)
break
else:
list_dp.append(1)
print(list_dp)