LeetCode 1626. Best Team With No Conflicts Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1626. Best Team With No Conflicts

Description

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 <= 1000
  • scores.length == ages.length
  • 1 <= scores[i] <= 106
  • 1 <= ages[i] <= 1000

Solutions

Solution 1

PythonJavaC++GoTypeScriptJavaScript
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)(code-box)

Solution 2

PythonJavaC++Go
class BinaryIndexedTree: def __init__(self, n): self.n = n self.c = [0] * (n + 1) def update(self, x, val): while x <= self.n: self.c[x] = max(self.c[x], val) x += x & -x def query(self, x): s = 0 while x: s = max(s, self.c[x]) x -= x & -x return s class Solution: def bestTeamScore(self, scores: List[int], ages: List[int]) -> int: m = max(ages) tree = BinaryIndexedTree(m) for score, age in sorted(zip(scores, ages)): tree.update(age, score + tree.query(age)) return tree.query(m)(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !