Best Team With No Conflicts

MediumDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Player i has score scores[i] and age ages[i]. You want to pick a team (any subset of players) with the largest total score. A team has a conflict if it includes a younger player whose score is **strictly greater** than the score of an older player. Players of the same age never conflict with each other, whatever their scores. Return the highest total score of a conflict-free team. O(n^2) is acceptable.

Examples

Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5]
Output: 34
Scores rise with age, so everyone can play: 1 + 3 + 5 + 10 + 15 = 34.
Input: scores = [4,5,6,5], ages = [2,1,2,1]
Output: 16
Take players 1, 2, and 3 (scores 5, 6, 5). The age-1 players score 5 and the age-2 player scores 6, so there is no conflict.
Input: scores = [1,2,3,5], ages = [8,9,10,1]
Output: 6
Player 3 (age 1, score 5) conflicts with every other player; the best is 1 + 2 + 3 = 6.

Constraints

  • 1 <= scores.length, ages.length <= 1000
  • scores.length == ages.length
  • 1 <= scores[i] <= 10^6
  • 1 <= ages[i] <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems