Table of Contents
Best Team: Problem Statement
You are the manager of a basketball team and your task is to form a best 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.
source: https://leetcode.com/problems/best-team-with-no-conflicts/description/
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 = [1,2,3,5], ages = [8,9,10,1]
Output: 6
Explanation: It is best to choose the first 3 players.
Java
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 |
class Solution { public int bestTeamScore(int[] scores, int[] ages) { Pair[] pairs = new Pair[ages.length]; for(int i=0;i<ages.length;i++){ pairs[i] = new Pair(ages[i], scores[i]); } //sort the players in ascending order based on their age //if two players age is same then sort them in ascending order based on their score Arrays.sort(pairs, new Comparator<Pair>(){ public int compare(Pair a, Pair b){ int res = Integer.compare(a.age, b.age); if(res==0){ res= Integer.compare(a.score, b.score); } return res; } }); Integer[][] memo = new Integer[ages.length][scores.length]; int m = dfs(pairs, -1, 0, memo); return m; } private int dfs(Pair[] pairs, int prev, int curr, Integer[][] memo){ if(curr >= pairs.length){ return 0; } if(memo[prev+1][curr]!=null) return memo[prev+1][curr]; //1. if we haven't choosen any player yet //2. compare the current player score with the previous player score if(prev==-1 || pairs[prev].score <= pairs[curr].score){ //if we choose the current player int a = pairs[curr].score + dfs(pairs, curr, curr+1, memo); //if we don't choose the current player int b = dfs(pairs, prev, curr+1, memo); int max = Math.max(a, b); memo[prev+1][curr] = max; return max; } //if both the abve conditions are not met int c = dfs(pairs, prev, curr+1, memo); memo[prev+1][curr] = c; return c; } static class Pair { int age; int score; public Pair(int age, int score){ this.age = age; this.score = score; } public String toString(){ return age+":"+score; } } } |
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Solution: def bestTeamScore(self, scores: List[int], ages: List[int]) -> int: combined = sorted(zip(ages, scores)) ln = len(scores) dp = [0] * ln dp[0] = combined[0][1] mx = dp[0] for i in range(1,ln): curr_age, curr_score = combined[i] for j in range(i-1, -1, -1): prev_age, prev_score = combined[j] if prev_score <= curr_score: dp[i] = max(dp[i], curr_score + dp[j]) else: ## prev_score > curr_score ## but this would only mean prev_age < curr_age (why?) ## hence dp[i] = max(dp[i], curr_score) mx = max(mx, dp[i]) #print(combined, dp) return mx |