【发布时间】:2020-07-03 16:11:45
【问题描述】:
您好,希望有人可以帮助我或至少给我正确方向的看法。
我有一个包含 100 到 500 个项目的数组。每个项目都有属性 name(string)、weight(float)、points(float)、position(int)。最大权重不是一个固定的数字,位置是(1,2,3,4,5)每个位置都需要填写。每个物品只能使用一次。结果应该是 7 个项目的最高分的 15 种组合。
据我所知,我的笔记本电脑因内存已满而崩溃。
我在 typescript 中尝试了一个使用 firebase 的解决方案。
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);
});
这里的项目只是它们的外观示例
感谢您的帮助
【问题讨论】:
-
您没有充分描述您要解决的问题。
The outcome should be the 15 combinations with the max value of 7 items.- 15 种组合是什么?什么是“最大值”? -
如果算法达不到标准,作为 firebase 函数运行对您没有多大帮助;仍然会有内存和时间限制。
-
好吧,我将值更改为积分。所以我需要7个项目的15个最佳点组合
标签: javascript typescript firebase