Javascript 背包问题还是排列问题?如何解决我的问题?
你好,希望有人能帮助我,或者至少给我正确的方向 我有一个数组,里面有100到500个项目。每个项目都有属性名称(字符串)、权重(浮动)、点(浮动)、位置(整数)。最大重量不是一个固定的数字,位置是(1,2,3,4,5),每个位置都需要填充。每个项目只能使用一次。结果应为15个组合,最大得分为7项 就我所知,我的笔记本电脑因为内存不足而崩溃了 我在typescript中尝试了一种使用firebase的解决方案Javascript 背包问题还是排列问题?如何解决我的问题?,javascript,typescript,firebase,Javascript,Typescript,Firebase,你好,希望有人能帮助我,或者至少给我正确的方向 我有一个数组,里面有100到500个项目。每个项目都有属性名称(字符串)、权重(浮动)、点(浮动)、位置(整数)。最大重量不是一个固定的数字,位置是(1,2,3,4,5),每个位置都需要填充。每个项目只能使用一次。结果应为15个组合,最大得分为7项 就我所知,我的笔记本电脑因为内存不足而崩溃了 我在typescript中尝试了一种使用firebase的解决方案 import * as functions from "firebase-functio
import * as functions from "firebase-functions";
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
export const potItems = functions.https.onRequest((request, response) => {
let possibleItems: any[] = [];
let counter: number = 0;
const items = [
{ name: "James Harden", position: "PG", weight: 15.9, points: 62.63 },
{ name: "Russell Westbrook", position: "PG", weight: 14.9, points: 56.12 },
{ name: "LeBron James", position: "SF", weight: 15.9, points: 55.67 },
{ name: "Bradley Beal", position: "SG", weight: 14.8, points: 52.69 },
{ name: "Anthony Davis", position: "PF", weight: 14.6, points: 52.16 },
{ name: "Damian Lillard", position: "PG", weight: 13.5, points: 49.34 },
{ name: "Nikola Vucevic", position: "C", weight: 12.9, points: 47.97 },
{ name: "Domantas Sabonis", position: "PF", weight: 12.8, points: 47.6 },
{ name: "Kristaps Porzingis", position: "PF", weight: 13.5, points: 47.18 },
{ name: "Andre Drummond", position: "C", weight: 12.2, points: 45.97 },
{ name: "Kawhi Leonard", position: "SF", weight: 13.2, points: 46.08 },
{ name: "Hassan Whiteside", position: "C", weight: 12.2, points: 45.83 },
{ name: "DeAndre Ayton", position: "C", weight: 11.8, points: 45.57 },
{ name: "Paul George", position: "SF", weight: 11.1, points: 43.8 },
{ name: "D'Angelo Russell", position: "PG", weight: 11.5, points: 44.0 },
{ name: "Julius Randle", position: "PF", weight: 11.3, points: 42.46 },
{ name: "DeMar DeRozan", position: "SG", weight: 11.2, points: 40.6 },
{ name: "Coby White", position: "PG", weight: 10.5, points: 37.85 },
{ name: "Ricky Rubio", position: "PG", weight: 11.1, points: 37.83 },
{ name: "Robert Covington", position: "SF", weight: 9.1, points: 37.26 },
{ name: "James Harden", position: "PG", weight: 15.9, points: 62.63 },
{ name: "Russell Westbrook", position: "PG", weight: 14.9, points: 56.12 },
{ name: "LeBron James", position: "SF", weight: 15.9, points: 55.67 },
{ name: "Bradley Beal", position: "SG", weight: 14.8, points: 52.69 },
{ name: "Anthony Davis", position: "PF", weight: 14.6, points: 52.16 },
{ name: "Damian Lillard", position: "PG", weight: 13.5, points: 49.34 },
{ name: "Nikola Vucevic", position: "C", weight: 12.9, points: 47.97 },
{ name: "Domantas Sabonis", position: "PF", weight: 12.8, points: 47.6 },
{ name: "Kristaps Porzingis", position: "PF", weight: 13.5, points: 47.18 },
{ name: "Andre Drummond", position: "C", weight: 12.2, points: 45.97 },
{ name: "Kawhi Leonard", position: "SF", weight: 13.2, points: 46.08 },
{ name: "Hassan Whiteside", position: "C", weight: 12.2, points: 45.83 },
{ name: "DeAndre Ayton", position: "C", weight: 11.8, points: 45.57 },
{ name: "Paul George", position: "SF", weight: 11.1, points: 43.8 },
{ name: "D'Angelo Russell", position: "PG", weight: 11.5, points: 44.0 },
{ name: "Julius Randle", position: "PF", weight: 11.3, points: 42.46 },
{ name: "DeMar DeRozan", position: "SG", weight: 11.2, points: 40.6 },
{ name: "Coby White", position: "PG", weight: 10.5, points: 37.85 },
{ name: "Ricky Rubio", position: "PG", weight: 11.1, points: 37.83 },
{ name: "Robert Covington", position: "SF", weight: 9.1, points: 37.26 }
];
for (let index0 = 0; index0 < items.length; index0++) {
for (let index1 = 0; index1 < items.length; index1++) {
if (index1 == index0) {
break;
}
for (let index2 = 0; index2 < items.length; index2++) {
if (index2 == index0 || index2 == index1) {
break;
}
for (let index3 = 0; index3 < items.length; index3++) {
if (index3 == index0 || index3 == index1 || index3 == index2) {
break;
}
for (let index4 = 0; index4 < items.length; index4++) {
if (
index4 == index0 ||
index4 == index1 ||
index4 == index2 ||
index4 == index3
) {
break;
}
for (let index5 = 0; index5 < items.length; index5++) {
if (
index5 == index0 ||
index5 == index1 ||
index5 == index2 ||
index5 == index3 ||
index5 == index4
) {
break;
}
for (let index6 = 0; index6 < items.length; index6++) {
if (
index6 == index0 ||
index6 == index1 ||
index6 == index2 ||
index6 == index3 ||
index6 == index4 ||
index6 == index5
) {
break;
}
let item: number[] = [];
item.push(index0);
item.push(index1);
item.push(index2);
item.push(index3);
item.push(index4);
item.push(index5);
item.push(index6);
item.sort((a, b) => a - b);
counter++;
possibleItems.push(counter);
console.log("item " + counter + ": " + item);
}
}
}
}
}
}
}
response.send("All items: " + possibleItems);
});
import*作为“firebase函数”中的函数;
////开始编写Firebase函数
// // https://firebase.google.com/docs/functions/typescript
//
export const potItems=functions.https.onRequest((请求,响应)=>{
让可能项:任意[]=[];
let计数器:数字=0;
常数项=[
{姓名:“詹姆斯·哈登”,位置:“PG”,体重:15.9,得分:62.63},
{姓名:“Russell Westbrook”,位置:“PG”,体重:14.9,分数:56.12},
{姓名:“勒布朗·詹姆斯”,位置:“SF”,体重:15.9,得分:55.67},
{姓名:“布拉德利·比尔”,位置:“SG”,体重:14.8,分:52.69},
{姓名:“安东尼·戴维斯”,位置:“PF”,体重:14.6,得分:52.16},
{姓名:“Damian Lillard”,位置:“PG”,体重:13.5,分数:49.34},
{姓名:“Nikola Vucevic”,位置:“C”,体重:12.9,分:47.97},
{姓名:“Domantas Sabonis”,体位:“PF”,体重:12.8,得分:47.6},
{姓名:“Kristaps Porzingis”,位置:“PF”,体重:13.5,分数:47.18},
{姓名:“Andre Drummond”,位置:“C”,重量:12.2,点数:45.97},
{姓名:“卡瓦希·伦纳德”,位置:“SF”,体重:13.2,分值:46.08},
{姓名:“Hassan Whiteside”,位置:“C”,重量:12.2,点数:45.83},
{姓名:“DeAndre Ayton”,位置:“C”,重量:11.8,点数:45.57},
{姓名:“Paul George”,位置:“SF”,体重:11.1,分数:43.8},
{姓名:“D'Angelo Russell”,位置:“PG”,体重:11.5,分数:44.0},
{姓名:“朱利叶斯·兰德尔”,位置:“PF”,体重:11.3,分数:42.46},
{名称:“DeMar DeRozan”,位置:“SG”,重量:11.2,点数:40.6},
{名称:“Coby White”,位置:“PG”,重量:10.5,点数:37.85},
{姓名:“里基·卢比奥”,位置:“PG”,体重:11.1,得分:37.83},
{姓名:“Robert Covington”,位置:“SF”,体重:9.1,分:37.26},
{姓名:“詹姆斯·哈登”,位置:“PG”,体重:15.9,得分:62.63},
{姓名:“Russell Westbrook”,位置:“PG”,体重:14.9,分数:56.12},
{姓名:“勒布朗·詹姆斯”,位置:“SF”,体重:15.9,得分:55.67},
{姓名:“布拉德利·比尔”,位置:“SG”,体重:14.8,分:52.69},
{姓名:“安东尼·戴维斯”,位置:“PF”,体重:14.6,得分:52.16},
{姓名:“Damian Lillard”,位置:“PG”,体重:13.5,分数:49.34},
{姓名:“Nikola Vucevic”,位置:“C”,体重:12.9,分:47.97},
{姓名:“Domantas Sabonis”,体位:“PF”,体重:12.8,得分:47.6},
{姓名:“Kristaps Porzingis”,位置:“PF”,体重:13.5,分数:47.18},
{姓名:“Andre Drummond”,位置:“C”,重量:12.2,点数:45.97},
{姓名:“卡瓦希·伦纳德”,位置:“SF”,体重:13.2,分值:46.08},
{姓名:“Hassan Whiteside”,位置:“C”,重量:12.2,点数:45.83},
{姓名:“DeAndre Ayton”,位置:“C”,重量:11.8,点数:45.57},
{姓名:“Paul George”,位置:“SF”,体重:11.1,分数:43.8},
{姓名:“D'Angelo Russell”,位置:“PG”,体重:11.5,分数:44.0},
{姓名:“朱利叶斯·兰德尔”,位置:“PF”,体重:11.3,分数:42.46},
{名称:“DeMar DeRozan”,位置:“SG”,重量:11.2,点数:40.6},
{名称:“Coby White”,位置:“PG”,重量:10.5,点数:37.85},
{姓名:“里基·卢比奥”,位置:“PG”,体重:11.1,得分:37.83},
{姓名:“Robert Covington”,位置:“SF”,体重:9.1,分:37.26}
];
对于(设index0=0;index0