简体   繁体   中英

Finding n-th combination with highest sum

I have a list of players and each player has a salary and a rating (both integer values).

I have to find n-th largest combination of 6 players (largest in terms of sum of their ratings) with a constraint that the sum of their salaries must be less or equal than 50000.

For example, if I have a list of players 1,2,...,m, what I'm currently doing is:

  1. Generate all possible 6 player combinations (m choose 6).
  2. Filter out combinations for which sum of salaries is > 50000
  3. Sort remaining combinations in descending order, ordered by sum of ratings
  4. Pick the n-th from the sorted list.

This is obviously a brute force approach which works fine for smaller number of players. But currently I have 140 players which yield over 9 billion combinations and it takes too much to finish.

Any suggestion on how to do this faster?

Here is how you can avoid getting all the combinations.

  • prepare a descending sorted map with rank as key and salary as value

This will get your ranks sorted on descending order and the first key in the map will be the highest rank. If you have multiple records having the same rank, consider putting them as a list against the same rank.

  • pick the first 6 top ranks and check if their total salary <= 50000, you get your result, else move to the next 6 combination.

Here if you have more than one record against a rank, try adding their salaries as well.

This will take some patience and some good testing to translate to a program, but will certainly be an optimal solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM