101 Logo
onenoughtone

Code Implementation

Best Team With No Conflicts Implementation

Below is the implementation of the best team with no conflicts:

solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Find the highest overall score of all possible basketball teams.
* Dynamic Programming (Longest Increasing Subsequence Variation) approach.
*
* @param {number[]} scores - Array of player scores
* @param {number[]} ages - Array of player ages
* @return {number} - Highest overall score
*/
function bestTeamScore(scores, ages) {
const n = scores.length;
// Create a list of pairs [age, score]
const players = [];
for (let i = 0; i < n; i++) {
players.push([ages[i], scores[i]]);
}
// Sort by age (and by score if ages are equal)
players.sort((a, b) => {
if (a[0] !== b[0]) {
return a[0] - b[0]; // Sort by age
}
return a[1] - b[1]; // Sort by score if ages are equal
});
// Initialize DP array
const dp = new Array(n);
for (let i = 0; i < n; i++) {
dp[i] = players[i][1]; // Initialize with the player's score
}
// Fill DP array
for (let i = 0; i < n; i++) {
for (let j = 0; j < i; j++) {
if (players[j][1] <= players[i][1]) { // If score of player j <= score of player i
dp[i] = Math.max(dp[i], dp[j] + players[i][1]);
}
}
}
// Return the maximum score
return Math.max(...dp);
}
/**
* Find the highest overall score of all possible basketball teams.
* Binary Indexed Tree (Fenwick Tree) approach.
*
* @param {number[]} scores - Array of player scores
* @param {number[]} ages - Array of player ages
* @return {number} - Highest overall score
*/
function bestTeamScoreBIT(scores, ages) {
const n = scores.length;
// Create a list of pairs [age, score]
const players = [];
for (let i = 0; i < n; i++) {
players.push([ages[i], scores[i]]);
}
// Sort by age (and by score if ages are equal)
players.sort((a, b) => {
if (a[0] !== b[0]) {
return a[0] - b[0]; // Sort by age
}
return a[1] - b[1]; // Sort by score if ages are equal
});
// Find the maximum score
const maxScore = Math.max(...scores);
// Initialize BIT
const bit = new Array(maxScore + 1).fill(0);
// Helper function to query BIT
function query(idx) {
let result = 0;
while (idx > 0) {
result = Math.max(result, bit[idx]);
idx -= idx & -idx; // Remove the least significant bit
}
return result;
}
// Helper function to update BIT
function update(idx, val) {
while (idx <= maxScore) {
bit[idx] = Math.max(bit[idx], val);
idx += idx & -idx; // Add the least significant bit
}
}
// Fill BIT
for (let i = 0; i < n; i++) {
const score = players[i][1];
const maxPrevScore = query(score);
update(score, maxPrevScore + score);
}
// Return the maximum score
return query(maxScore);
}
// Test cases
console.log(bestTeamScore([1, 3, 5, 10, 15], [1, 2, 3, 4, 5])); // 34
console.log(bestTeamScore([4, 5, 6, 5], [2, 1, 2, 1])); // 16
console.log(bestTeamScore([1, 2, 3, 5], [8, 9, 10, 1])); // 6
console.log(bestTeamScoreBIT([1, 3, 5, 10, 15], [1, 2, 3, 4, 5])); // 34
console.log(bestTeamScoreBIT([4, 5, 6, 5], [2, 1, 2, 1])); // 16
console.log(bestTeamScoreBIT([1, 2, 3, 5], [8, 9, 10, 1])); // 6

Step-by-Step Explanation

Let's break down the implementation:

  1. Create Player Pairs: Create a list of pairs [age, score] for each player to keep track of both attributes together.
  2. Sort Players: Sort the players by age in ascending order (and by score in ascending order if ages are equal) to ensure we only consider valid teams.
  3. Initialize DP Array: Initialize a DP array where dp[i] represents the maximum score we can achieve by considering the first i players and including the i-th player.
  4. Fill DP Array: For each player, consider all previous players with scores less than or equal to the current player's score, and update the DP array accordingly.
  5. Find Maximum Score: Return the maximum value in the DP array as the highest overall score of all possible basketball teams.
ProblemSolutionCode
101 Logo
onenoughtone