|
| 1 | +/** |
| 2 | + * 1626. Best Team With No Conflicts |
| 3 | + * https://leetcode.com/problems/best-team-with-no-conflicts/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * You are the manager of a basketball team. For the upcoming tournament, you want to choose the |
| 7 | + * team with the highest overall score. The score of the team is the sum of scores of all the |
| 8 | + * players in the team. |
| 9 | + * |
| 10 | + * However, the basketball team is not allowed to have conflicts. A conflict exists if a younger |
| 11 | + * player has a strictly higher score than an older player. A conflict does not occur between |
| 12 | + * players of the same age. |
| 13 | + * |
| 14 | + * Given two lists, scores and ages, where each scores[i] and ages[i] represents the score and |
| 15 | + * age of the ith player, respectively, return the highest overall score of all possible basketball |
| 16 | + * teams. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @param {number[]} scores |
| 21 | + * @param {number[]} ages |
| 22 | + * @return {number} |
| 23 | + */ |
| 24 | +var bestTeamScore = function(scores, ages) { |
| 25 | + const players = ages.map((age, index) => ({ age, score: scores[index] })); |
| 26 | + players.sort((a, b) => a.age - b.age || a.score - b.score); |
| 27 | + |
| 28 | + const maxScores = new Array(players.length).fill(0); |
| 29 | + let highestScore = 0; |
| 30 | + |
| 31 | + for (let current = 0; current < players.length; current++) { |
| 32 | + maxScores[current] = players[current].score; |
| 33 | + for (let previous = 0; previous < current; previous++) { |
| 34 | + if (players[previous].score <= players[current].score) { |
| 35 | + maxScores[current] = Math.max( |
| 36 | + maxScores[current], |
| 37 | + maxScores[previous] + players[current].score |
| 38 | + ); |
| 39 | + } |
| 40 | + } |
| 41 | + highestScore = Math.max(highestScore, maxScores[current]); |
| 42 | + } |
| 43 | + |
| 44 | + return highestScore; |
| 45 | +}; |
0 commit comments