我有这个清单
import datetime
sample_list = [{'start_date': datetime.datetime(2017, 8, 18), 'end_date': datetime.datetime(2017, 8, 25), 'value': 20},
{'start_date': datetime.datetime(2017, 8, 19), 'end_date': datetime.datetime(2017, 8, 25), 'value': 22},
{'start_date': datetime.datetime(2017, 8, 24), 'end_date': datetime.datetime(2017, 8, 30), 'value': 40},
{'start_date': datetime.datetime(2017, 8, 25), 'end_date': datetime.datetime(2017, 8, 26), 'value': 52},
{'start_date': datetime.datetime(2017, 8, 27), 'end_date': datetime.datetime(2017, 8, 29), 'value': 12},
{'start_date': datetime.datetime(2017, 9, 1), 'end_date': datetime.datetime(2017, 9, 5), 'value': 20}
]
并希望生成optimum_list
,其内容表示value
的最大和,这样每个成员的日期就不会相互重叠
上面提供的sample_list
的期望输出是
optimum_list =
{'start_date': datetime.datetime(2017, 8, 25), 'end_date': datetime.datetime(2017, 8, 26), 'value': 52},
{'start_date': datetime.datetime(2017, 8, 27), 'end_date': datetime.datetime(2017, 8, 29), 'value': 12},
{'start_date': datetime.datetime(2017, 9, 1), 'end_date': datetime.datetime(2017, 9, 5), 'value': 20}
cumulative_value_of_sum = 84
有没有办法有效地解决这个问题
O(len(data)^2)
使用动态规划的解决方案:编辑:需要最小化的第二个值的修改解决方案:
相关问题 更多 >
编程相关推荐