动态规划 背包问题
以上方经典问题举例,以下为代码实现:
function bag(weights, value, W) {
var n = weights.length - 1
var f = [[]]
for (var j = 0; j <= W; j++) {
if (j < weights[0]) {
f[0][j] = 0
} else {
f[0][j] = value[0]
}
}
for (var j = 0; j <= W; j++) {
for (var i = 1; i <= n; i++) {
if (!f[i]) {
f[i] = []
}
if (j < weights[i]) {
f[i][j] = f[i - 1][j]
} else {
f[i][j] = Math.max(f[i - 1][j], f[i - 1][j - weights[i]] + value[i])
}
}
}
return f[n][W]
}
console.log(bag([2, 2, 6, 5, 4], [6, 3, 5, 4, 6], 10))
ok,打印结果为15 至此问题解决。
什么是背包问题
好家伙,不清楚的小伙伴肯定问了,什么是™的背包问题,什么是动态规划啊?什么也不说直接给答案是吧,咱们这里对于算法不过于纠结。
只是简单说一下题目:背包问题就是,给出的weights参数呢就是各个将要放入背包里的物体的重量,value呢就是指各个物体的价值(你可以理解为代表多少软妹币),
讲人话:以第一个数组第一项2,第二个数组第一项6为例,意思就是这个物体的重量为2kg,可以卖6块钱(有个人前来买瓜 dog)
那么好,大家伙肯定都理解什么意思了,就是说,我这个背包(也就是W)最多可以放10kg的东西,那么在不超出这个重量的前提下,最高可以获得的价钱是多少。
好,让Ai给解释一下上面代码啥意思
This code is an implementation of the 0-1 Knapsack problem. It takes in an array of weights, an array of values, and a maximum weight as parameters and returns the maximum value that can be obtained from the given items without exceeding the maximum weight.
Step by step explanation:
- The function bag() takes in three parameters: an array of weights, an array of values, and a maximum weight (W).
- The variable n is set to the length of the weights array minus one.
- An empty 2D array f is declared.
- A for loop is used to iterate through the range of 0 to W. If j is less than the first weight in the array, the value at index [0][j] of the array f is set to 0. Otherwise, the value at index [0][j] of the array f is set to the value of the first item in the value array.
- Another for loop is used to iterate through the range of 0 to W. A nested for loop is used to iterate through the range of 1 to n.
- If the index f[i] does not exist, it is initialized as an empty array.
- If j is less than the weight of the item at index i, the value at index [i][j] of the array f is set to the value at index [i-1][j] of the array f. Otherwise, the value at index [i][j] of the array f is set to the maximum of the value at index [i-1][j] of the array f and the value at index [i-1][j-weights[i]] of the array f plus the value of the item at index i of the value array.
- The function returns the value at index [n][W] of the array f.
- The function is called with the arrays [2,2,6,5,4] and [6,3,5,4,6] as weights and values respectively, and 10 as the maximum weight. The result is logged to the console.
好家伙,这解释的是真够清楚的,每块代码啥意思,包括参数都给你说的明明白白,(原来不懂你就百度,现在直接一个985 211给你讲代码是吧,舒服了)
行,你觉得英文解释还是不够直观是吧,翻译
此代码是 0-1 背包问题的实现。 它接受一个权重数组、一个值数组和一个最大权重作为参数,并返回在不超过最大权重的情况下可以从给定项目中获得的最大值。 一步一步的解释:
- 函数 bag() 接受三个参数:权重数组、值数组和最大权重 (W)。
- 变量 n 被设置为权重数组的长度减一。
- 声明了一个空的二维数组 f。
- for循环用于遍历0到W的范围。如果j小于数组中的第一个权重,则将数组f的索引[0][j]处的值设置为0。否则, 数组 f 的索引 [0][j] 处的值设置为值数组中第一项的值。
- 另一个for循环用于遍历0到W的范围。嵌套的for循环用于遍历1到n的范围。
- 如果索引f[i]不存在,则初始化为空数组。
- 如果 j 小于索引 i 处的项目的权重,则将数组 f 的索引 [i][j] 处的值设置为数组 f 的索引 [i-1][j] 处的值。 否则,数组 f 索引 [i][j] 处的值设置为数组 f 索引 [i-1][j] 处的值与索引 [i-1][j] 处的值中的最大值 数组 f 的 -weights[i]] 加上值数组的索引 i 处的项目的值。
- 函数返回数组 f 的索引 [n][W] 处的值。
- 以数组[2,2,6,5,4]和[6,3,5,4,6]分别作为权重和值调用函数,最大权重为10。 结果记录到控制台。
好,相信大家伙都能看明白每步骤啥意思,但是第7步的否则开始,肯定是懵圈的,啥意思啊,你懵我也懵,所以有时候翻译并不准确,你就要看原文,Otherwise, the value at index [i][j] of the array f is set to the maximum of the value at index [i-1][j] of the array f and the value at index [i-1][j-weights[i]] of the array f plus the value of the item at index i of the value array. 这段话啥意思,(否则,在f的索引为[i][j]的位置,的值,要设为 当前列的上一行索引的value值 和 上一行的第[当前背包重量-当前物品重量]列的value值 加上当前物品的value值)两者其中的最大值
好,大伙仔细品一下 和 字两边我加重的两句话 与对应的实现代码的位置Math.max(f[i-1][j],f[i-1][j-weights[i]]+value[i]),也就是逗号两端分别对应。这个公式是解决此问题的关键。
最后,总结
相信大家伙肯定也可以从各个地方找到各种资料,那么最方便的最直接的就是AI给出的说明文档,如果能直接理解英文文档是最好,如果不能就翻译也无可厚非,但是个别翻译不准确的地方你要再重新捋一下是什么意思。
不是为了蹭AI的热度,以前都是互联网+,现在都是AI+,AI衍生出来的产品层数不穷,带来的效率和裁员也肉眼可见,AI帮助我们提高效率(当然有人就会被优化),就像有汽车可以代步,你还会选择骑马代步吗,并不会,我们能做的就是从原来的学会骑马变成学会开车,仅此而已。
当然了,个人还佩服那些造车的人,以上仅为个人见解,希望是授人渔而不是授人鱼。本人更新文章完全是看缘分,有时候想到对大伙有帮助的东西就写一写,希望能帮到更多的人,有更好的学习方法大伙可以评论区交流。