|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +-* 1626. Best Team With No Conflicts *- |
| 5 | +
|
| 6 | +
|
| 7 | +
|
| 8 | +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. |
| 9 | +
|
| 10 | +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. |
| 11 | +
|
| 12 | +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. |
| 13 | +
|
| 14 | + |
| 15 | +
|
| 16 | +Example 1: |
| 17 | +
|
| 18 | +Input: scores = [1,3,5,10,15], ages = [1,2,3,4,5] |
| 19 | +Output: 34 |
| 20 | +Explanation: You can choose all the players. |
| 21 | +Example 2: |
| 22 | +
|
| 23 | +Input: scores = [4,5,6,5], ages = [2,1,2,1] |
| 24 | +Output: 16 |
| 25 | +Explanation: It is best to choose the last 3 players. Notice that you are allowed to choose multiple people of the same age. |
| 26 | +Example 3: |
| 27 | +
|
| 28 | +Input: scores = [1,2,3,5], ages = [8,9,10,1] |
| 29 | +Output: 6 |
| 30 | +Explanation: It is best to choose the first 3 players. |
| 31 | + |
| 32 | +
|
| 33 | +Constraints: |
| 34 | +
|
| 35 | +1 <= scores.length, ages.length <= 1000 |
| 36 | +scores.length == ages.length |
| 37 | +1 <= scores[i] <= 106 |
| 38 | +1 <= ages[i] <= 1000 |
| 39 | +
|
| 40 | +*/ |
| 41 | + |
| 42 | +import 'dart:math'; |
| 43 | + |
| 44 | +class Player { |
| 45 | + late int age; |
| 46 | + late int score; |
| 47 | + Player(int age, int score) { |
| 48 | + this.age = age; |
| 49 | + this.score = score; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +class A { |
| 54 | + // List<List<int>> dp = |
| 55 | + // List.filled(1005, 0).map((e) => List.filled(1005, 0)).toList(); |
| 56 | + int bestTeamScore(List<int> scores, List<int> ages) { |
| 57 | + final int n = scores.length; |
| 58 | + List<Player> players = []; |
| 59 | + // dp[i] := max score of choosing players[0..i] w/ players[i] being selected |
| 60 | + List<int> dp = List.filled(n, 0); |
| 61 | + |
| 62 | + for (int i = 0; i < n; ++i) players[i] = new Player(ages[i], scores[i]); |
| 63 | + |
| 64 | + players.sort((a, b) => a.age == b.age ? b.score - a.score : b.age - a.age); |
| 65 | + |
| 66 | + for (int i = 0; i < n; ++i) { |
| 67 | + // For each player, we choose it first |
| 68 | + dp[i] = players[i].score; |
| 69 | + // players[j].age >= players[i].age since we sort in descending order |
| 70 | + // So we only have to check that |
| 71 | + // players[j].score >= players[i].score |
| 72 | + for (int j = 0; j < i; ++j) |
| 73 | + if (players[j].score >= players[i].score) |
| 74 | + dp[i] = max(dp[i], dp[j] + players[i].score); |
| 75 | + } |
| 76 | + |
| 77 | + return dp[n]; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +class B { |
| 82 | + int bestTeamScore(List<int> scores, List<int> ages) { |
| 83 | + int n = scores.length; |
| 84 | + List<List<int>> players = |
| 85 | + List.filled(n, 0).map((e) => List.filled(2, 0)).toList(); |
| 86 | + |
| 87 | + for (int i = 0; i < n; i++) { |
| 88 | + players[i][0] = scores[i]; |
| 89 | + players[i][1] = ages[i]; |
| 90 | + } |
| 91 | + |
| 92 | + // Sorting Players on the basis of scores(Ascending) and break their ties on the basis of age(Ascending) |
| 93 | + players.sort( |
| 94 | + (a, b) => a[0] == b[0] ? a[1].compareTo(b[1]) : a[0].compareTo(b[0])); |
| 95 | + |
| 96 | + List<int> dp = List.filled(n, 0); |
| 97 | + int maxi = 0; |
| 98 | + for (int i = 0; i < n; i++) { |
| 99 | + dp[i] = search(dp, players, i) + players[i][0]; |
| 100 | + maxi = max(maxi, dp[i]); |
| 101 | + } |
| 102 | + |
| 103 | + return maxi; |
| 104 | + } |
| 105 | + |
| 106 | + int search(List<int> dp, List<List<int>> players, int i) { |
| 107 | + int maxi = 0; |
| 108 | + for (int j = 0; j < i; j++) { |
| 109 | + if (dp[j] > maxi && players[j][1] <= players[i][1]) { |
| 110 | + maxi = dp[j]; |
| 111 | + } |
| 112 | + } |
| 113 | + return maxi; |
| 114 | + } |
| 115 | +} |
0 commit comments