fix(learn): correct test result (#41068)
This commit is contained in:
@ -73,7 +73,7 @@ assert.equal(
|
|||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
`knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 35, 0.35)` should return `75300`.
|
`knapsackUnbounded([{ name:"panacea", value:3000, weight:0.3, volume:0.025 }, { name:"ichor", value:1800, weight:0.2, volume:0.015 }, { name:"gold", value:2500, weight:2, volume:0.002 }], 35, 0.35)` should return `75900`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
assert.equal(
|
assert.equal(
|
||||||
@ -86,7 +86,7 @@ assert.equal(
|
|||||||
35,
|
35,
|
||||||
0.35
|
0.35
|
||||||
),
|
),
|
||||||
75300
|
75900
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -120,36 +120,66 @@ function knapsackUnbounded(items, maxweight, maxvolume) {
|
|||||||
# --solutions--
|
# --solutions--
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function knapsackUnbounded(items, maxweight, maxvolume) {
|
function knapsackUnbounded(items, maxWeight, maxVolume) {
|
||||||
var n = items.length;
|
function getPickTotals(items, pick) {
|
||||||
var best_value = 0;
|
let totalValue = 0;
|
||||||
var count = new Array(n);
|
let totalWeight = 0;
|
||||||
var best = new Array(n);
|
let totalVolume = 0;
|
||||||
function recurseKnapsack(i, value, weight, volume) {
|
for (let i = 0; i < items.length; i++) {
|
||||||
var j, m1, m2, m;
|
totalValue += pick[i] * items[i].value;
|
||||||
if (i == n) {
|
totalWeight += pick[i] * items[i].weight;
|
||||||
if (value > best_value) {
|
totalVolume += pick[i] * items[i].volume;
|
||||||
best_value = value;
|
|
||||||
for (j = 0; j < n; j++) {
|
|
||||||
best[j] = count[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
m1 = Math.floor(weight / items[i].weight);
|
return [totalValue, totalWeight, totalVolume];
|
||||||
m2 = Math.floor(volume / items[i].volume);
|
}
|
||||||
m = m1 < m2 ? m1 : m2;
|
|
||||||
for (count[i] = m; count[i] >= 0; count[i]--) {
|
function getMaxes(items, maxWeight, maxVolume) {
|
||||||
recurseKnapsack(
|
const maxes = [];
|
||||||
i + 1,
|
for (let i = 0; i < items.length; i++) {
|
||||||
value + count[i] * items[i].value,
|
const maxUnitsInWeight = Math.floor(maxWeight / items[i].weight);
|
||||||
weight - count[i] * items[i].weight,
|
const maxUnitsInVolume = Math.floor(maxVolume / items[i].volume);
|
||||||
volume - count[i] * items[i].volume
|
const maxUnitsInLimit = Math.min(maxUnitsInWeight, maxUnitsInVolume);
|
||||||
);
|
maxes.push(maxUnitsInLimit);
|
||||||
|
}
|
||||||
|
return maxes;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isInLimit(value, limit) {
|
||||||
|
return value <= limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCombinations(maxValues, curPicks, combinations) {
|
||||||
|
if (maxValues.length === 0) {
|
||||||
|
combinations.push(curPicks);
|
||||||
|
}
|
||||||
|
|
||||||
|
const curMax = maxValues[0];
|
||||||
|
const leftMaxValues = maxValues.slice(1);
|
||||||
|
for (let i = 0; i <= curMax; i++) {
|
||||||
|
getCombinations(leftMaxValues, curPicks.concat(i), combinations);
|
||||||
|
}
|
||||||
|
return combinations;
|
||||||
|
}
|
||||||
|
|
||||||
|
let bestValue = 0;
|
||||||
|
let bestPick = [];
|
||||||
|
const maxes = getMaxes(items, maxWeight, maxVolume);
|
||||||
|
const combinations = getCombinations(maxes, [], []);
|
||||||
|
for (let i = 0; i < combinations.length; i++) {
|
||||||
|
const curPick = combinations[i];
|
||||||
|
const [curValue, curWeight, curVolume] = getPickTotals(items, curPick);
|
||||||
|
if (!isInLimit(curWeight, maxWeight) || !isInLimit(curVolume, maxVolume)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (curValue > bestValue) {
|
||||||
|
bestValue = curValue;
|
||||||
|
bestPick = [curPick];
|
||||||
|
} else if (curValue === bestValue) {
|
||||||
|
bestPick.push(curPick);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
recurseKnapsack(0, 0, maxweight, maxvolume);
|
return bestValue;
|
||||||
return best_value;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user