admin管理员组文章数量:1391991
Let’s say we have a list of intervals, each of which has a cost associated with it. Assume that there is always at least one interval active. The solution would be a timeline of non overlapping intervals with the minimum cost at any point in time.
For example, for a list intervals of the type [start, end, value]
[
[2024-01-01, 2024-05-01, 500],
[2024-01-01, 2024-03-01, 400],
[2024-02-01, 2024-04-01, 300],
[2024-03-01, 2024-04-01, 200]
]
Then the solution would be
[
[2024-01-01, 2024-02-01, 400],
[2024-02-01, 2024-03-01, 300],
[2024-03-01, 2024-04-01, 200],
[2024-04-01, 2024-05-01, 500]
]
I do have a variant of the sweeping line algorithm, although I am not sure it is the best way to solve this problem. In that solution, I have to iterate through each start and end point of every interval (in order), keep track of the active intervals, and at each point, choose the minimum cost among them. This runs in O(n^2).
Edit: @pjs gave me an idea of using a priority queue. This will be give us a better time complexity for choosing the minimum cost at each point. This will run in O(n log(n)).
I am wondering if there is any other algorithms for this? I’ve looked into scheduling algorithms but these do not seem to fit my problem exactly.
版权声明:本文标题:algorithm - Finding the minimum cost at each point in time from a set of weighted intervals - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744745012a2622822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论