【发布时间】:2021-01-26 02:44:24
【问题描述】:
谁能提供具有时间复杂度的最佳解决方案?非常感谢!
确定运行预定视频流所需的带宽。
可能有成千上万个流,并且所有调度数据在开始时都是可用的。
可能存在没有流运行的时间间隔
输入
int startTime; //start time for the stream
int duration; //how long the stream runs
int bandwidth; // consumed bandwidth from start to end
输出:
list[int] totalBW; // bandwidth needed at every time instant
示例:
输入(列表可能未排序)
[ [0,2,10], [1,2,10], [2,1,20] ]
输出
[10, 20, 30]
说明
At time 0: only the first element needs bandwidth 10 => [10, ...]
At time 1: first two elements need bandwidth 10 + 10 => [10, 20, ...]
At time 2: the second and third element need bandwidth 10 + 20 => [10, 20, 30]
使用python的蛮力方法:
def solution(streams):
_max = max(streams, key=lambda x: (x[0]+x[1]))
_max_time = _max[0] + _max[1]
res = [0] * _max_time
for s, d, bw in streams:
for i in range(d):
res[s+i] += bw
return res
有没有更有效的方法?
【问题讨论】:
标签: optimization