[英]Given an elevator with max weight and n people with x_i weights, find out minimum number of rides needed
input:
max_weight = 550
n = 4
x_i = [120, 175, 250, 150]
output:
2
// [[250, 175, 120], [150]]
My initial impression is that this looks very similar to a dynamic programming coin change/knapsack problem, however it is not coin change (which would be asking for the fewest number of weights to make an exact amount), and it is not knapsack (the weights do not have values and it's like I can have more than 1 knapsack). 我最初的印象是,这看起来与动态编程的找零/背负问题非常相似,但是它不是找零(这将要求最少的砝码数量才能准确确定重量),也不是背负(砝码没有值,就像我可以装多个背包。
Is there a common name/solution for this problem? 这个问题是否有通用名称/解决方案?
This is actually a (1D) Bin Packing problem : 这实际上是(1D)装箱问题 :
In the bin packing problem, objects of different volumes must be packed into a finite number of bins or containers each of volume V in a way that minimizes the number of bins used . 在垃圾箱包装问题中,必须以最小化使用的垃圾箱数量的方式, 将 不同体积的物体包装到有限数量的垃圾箱或容器中,每个垃圾箱或容器的体积为V。 In computational complexity theory, it is a combinatorial NP-hard problem. 在计算复杂度理论中,它是一个组合的NP难题。
Here the persons map on the objects en the bins on the rides. 在这里,人员将物体映射到游乐设施上的垃圾箱中。 Like the bin packing problem we want to minimize the number of rides "used" and each person occupies a certain "volume" (that person's weight). 像垃圾箱包装问题一样,我们要尽量减少“使用”的游乐设施的数量,并且每个人都占据一定的“体积”(该人的体重)。
The bin packing problem is - as said in the article - NP-hard. 如文章所述,垃圾箱包装问题是NP-hard。 We can use dynamic programming (but it still has - worst case - exponential time). 我们可以使用动态编程(但它仍然具有-最坏的情况-指数时间)。
The paper A New Algorithm for Optimal Bin Packing by Richard E. Korf discusses an algorithm to solve this problem exactly. 理查德·科夫 ( Richard E. Korf)的《 最佳装箱的新算法 》一文讨论了一种精确解决该问题的算法。 It works by using a heuristic approach first and calculating a lower bound, and then use branch and bound to iteratively derive a better solution than the heuristic one until the lower bound is reached, or no solution can be found anymore. 它的工作方式是先使用启发式方法并计算下限,然后使用branch and bound迭代地得出比启发式方法更好的解决方案,直到达到下界,或者找不到解决方案为止。
You're on the right track. 您走在正确的轨道上。 The problem is solved by a modified coin-change algorithm: rather than demanding an exact solution, you find the one that attains the highest total without exceeding the target amount. 通过改进的硬币找零算法可以解决该问题:您无需找到确切的解决方案,而是找到了总额最高而又不超过目标金额的解决方案。 Of course, if you find a solution whose shortfall is less than any remaining set element, you stop and report that solution. 当然,如果找到短缺量小于任何剩余set元素的解决方案,则停止并报告该解决方案。
When you find a solution, remove the "used" weights and iterate until you have allocated them all. 找到解决方案后,请删除“已使用”的权重并进行迭代,直到将所有权重分配完毕为止。 The number of iterations is the quantity of elevators you need. 迭代次数是您需要的电梯数量。
If you sort the elements in descending order, this gives you a "greedy" beginning with backtracking. 如果按降序对元素进行排序,则从回溯开始会给您“贪婪”的感觉。 I suspect that this is reasonably close to optimal for many cases, especially if you memoize the results so that you don't repeat mistakes on the next iteration. 我怀疑这在很多情况下都接近最佳,尤其是当您记住结果以便在下一次迭代中不会重复出现错误时。
You might try some pathological cases, such as a limit of 100, and extreme weights like 您可能会尝试一些病理情况,例如限制为100,以及极端的体重,例如
[93, 91, ..., 5, 4, 4]
The "greedy" algorithm goes for 93+5 first, but later settles for 91+4+4 (closer solution). “贪心”算法首先适用于93 + 5,但后来适用于91 + 4 + 4(更严格的解决方案)。 This is where the memoization comes in handy. 这是方便使用备忘录的地方。
Does that move you toward a solution? 这会推动您走向解决方案吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.