Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the sum of scores of all the players in the team.
However, the basketball team is not allowed to have conflicts. A conflict exists if a younger player has a strictly higher score than an older player. A conflict does not occur between players of the same age.
Given two lists, scores and ages, where each scores[i] and ages[i] represents the score and age of the ith player, respectively, return the highest overall score of all possible basketball teams.
Example 1:
Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5] Output: 34 Explanation: You can choose all the players.
Example 2:
Input: scores = [4,5,6,5], ages = [2,1,2,1] Output: 16 Explanation: It is best to choose the last 3 players. Notice that you are allowed to choose multiple people of the same age.
Example 3:
Input: scores = [1,2,3,5], ages = [8,9,10,1] Output: 6 Explanation: It is best to choose the first 3 players.
Constraints:
1 <= scores.length, ages.length <= 1000scores.length == ages.length1 <= scores[i] <= 1061 <= ages[i] <= 1000Problem summary: You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the sum of scores of all the players in the team. However, the basketball team is not allowed to have conflicts. A conflict exists if a younger player has a strictly higher score than an older player. A conflict does not occur between players of the same age. Given two lists, scores and ages, where each scores[i] and ages[i] represents the score and age of the ith player, respectively, return the highest overall score of all possible basketball teams.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,3,5,10,15] [1,2,3,4,5]
[4,5,6,5] [2,1,2,1]
[1,2,3,5] [8,9,10,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1626: Best Team With No Conflicts
class Solution {
public int bestTeamScore(int[] scores, int[] ages) {
int n = ages.length;
int[][] arr = new int[n][2];
for (int i = 0; i < n; ++i) {
arr[i] = new int[] {scores[i], ages[i]};
}
Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
int[] f = new int[n];
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (arr[i][1] >= arr[j][1]) {
f[i] = Math.max(f[i], f[j]);
}
}
f[i] += arr[i][0];
ans = Math.max(ans, f[i]);
}
return ans;
}
}
// Accepted solution for LeetCode #1626: Best Team With No Conflicts
func bestTeamScore(scores []int, ages []int) int {
n := len(ages)
arr := make([][2]int, n)
for i := range ages {
arr[i] = [2]int{scores[i], ages[i]}
}
sort.Slice(arr, func(i, j int) bool {
a, b := arr[i], arr[j]
return a[0] < b[0] || a[0] == b[0] && a[1] < b[1]
})
f := make([]int, n)
for i := range arr {
for j := 0; j < i; j++ {
if arr[i][1] >= arr[j][1] {
f[i] = max(f[i], f[j])
}
}
f[i] += arr[i][0]
}
return slices.Max(f)
}
# Accepted solution for LeetCode #1626: Best Team With No Conflicts
class Solution:
def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
arr = sorted(zip(scores, ages))
n = len(arr)
f = [0] * n
for i, (score, age) in enumerate(arr):
for j in range(i):
if age >= arr[j][1]:
f[i] = max(f[i], f[j])
f[i] += score
return max(f)
// Accepted solution for LeetCode #1626: Best Team With No Conflicts
struct Solution;
impl Solution {
fn best_team_score(scores: Vec<i32>, ages: Vec<i32>) -> i32 {
let n = scores.len();
let mut players = vec![];
for i in 0..n {
players.push((ages[i], scores[i]));
}
players.sort_unstable();
let mut dp = vec![0; n];
for i in 0..n {
dp[i] = players[i].1;
for j in 0..i {
if players[j].1 <= players[i].1 {
dp[i] = dp[i].max(players[i].1 + dp[j]);
}
}
}
dp.into_iter().max().unwrap()
}
}
#[test]
fn test() {
let scores = vec![1, 3, 5, 10, 15];
let ages = vec![1, 2, 3, 4, 5];
let res = 34;
assert_eq!(Solution::best_team_score(scores, ages), res);
let scores = vec![4, 5, 6, 5];
let ages = vec![2, 1, 2, 1];
let res = 16;
assert_eq!(Solution::best_team_score(scores, ages), res);
let scores = vec![1, 2, 3, 5];
let ages = vec![8, 9, 10, 1];
let res = 6;
assert_eq!(Solution::best_team_score(scores, ages), res);
}
// Accepted solution for LeetCode #1626: Best Team With No Conflicts
function bestTeamScore(scores: number[], ages: number[]): number {
const arr = ages.map((age, i) => [age, scores[i]]);
arr.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]));
const n = arr.length;
const f = new Array(n).fill(0);
for (let i = 0; i < n; ++i) {
for (let j = 0; j < i; ++j) {
if (arr[i][1] >= arr[j][1]) {
f[i] = Math.max(f[i], f[j]);
}
}
f[i] += arr[i][1];
}
return Math.max(...f);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.