JavaScript实现符合薪资限制的最优球员组合生成算法
JavaScript Solution for Optimal Team Combination (With Salary Cap)
Context & Data
First, let's lay out the core data and constraints clearly:
- Required positions:
goalkeeper,midfilder(note: this might be a typo for "midfielder", but we'll stick with the given term) - Player dataset (corrected to valid JSON format):
[ {"name": "A", "position": "goalkeeper", "salary": 300, "rating" : 9}, {"name": "B", "position": "goalkeeper", "salary": 200, "rating" : 8 }, {"name": "C", "position": "midfilder", "salary": 400 , "rating" : 9}, {"name": "D", "position": "midfilder", "salary": 300 , "rating" : 7}, {"name": "E", "position": "striker", "salary": 400, "rating" : 9 }, {"name": "F", "position": "striker", "salary": 200 ,"rating" : 7} ]
Requirements
We need to generate all valid team combinations that meet these rules:
- Must include exactly one player from each of the three positions
- Total team salary must not exceed 1100
- Results should be sorted by total rating in descending order (higher ratings take priority)
- Output must follow this structure (example):
[ { "team" : ["A","C","E"], "total_salary" : 1100, "total_rating" : 27 }, { "team" : ["B","C","E"], "total_salary" : 1000, "total_rating" : 26 }, { "team" : ["A","D","E"], "total_salary" : 1000, "total_rating" : 25 }, // ... remaining valid combinations ]
Current Status
A C language implementation of this logic already exists, and we need to translate this into a working JavaScript solution.
内容的提问来源于stack exchange,提问作者Suraj
相关产品推荐
相关产品推荐

