Skip to content

Commit 4ca5a8b

Browse files
committed
leetcode
1 parent afc6fed commit 4ca5a8b

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import "sort"
4+
5+
func max(a, b int) int {
6+
if a > b {
7+
return a
8+
}
9+
return b
10+
}
11+
12+
func bestTeamScore(scores []int, ages []int) int {
13+
var n int = len(ages)
14+
player := [][]int{}
15+
for i := 0; i < n; i++ {
16+
player = append(player, []int{ages[i], scores[i]})
17+
}
18+
sort.Slice(player, func(i, j int) bool {
19+
return player[i][0] < player[j][0] || (player[i][0] == player[j][0] && player[i][1] < player[j][1])
20+
})
21+
var ans int = 0
22+
DP := make([]int, n+1)
23+
for i := 0; i < n; i++ {
24+
DP[i+1] = player[i][1]
25+
for j := 0; j < i; j++ {
26+
if player[j][1] > player[i][1] {
27+
continue
28+
}
29+
DP[i+1] = max(DP[i+1], player[i][1]+DP[j+1])
30+
}
31+
ans = max(ans, DP[i+1])
32+
}
33+
return ans
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 🔥 FAST 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Intuition
4+
5+
Sorting with DP.
6+
7+
## Approach
8+
9+
Sort the Players on the basis of scores(Ascending) and break their ties on the basis of age(Ascending).
10+
11+
Now, we can select the players from left to right.
12+
13+
Create a dp array of length n, where dp[i], indicates the sum of scores of selected players on left of ith player + score of ith player.
14+
15+
For each player i, now search a player j on left which has maximum dp[j] value and age[j]<=age[i].
16+
17+
Fill dp[i] with searched value(if not found then 0) + current player score.
18+
19+
Get the max from dp.
20+
21+
## Complexity
22+
23+
### Time complexity: O(n^2)
24+
25+
### Space complexity: O(n)
26+
27+
```dart
28+
class Solution {
29+
int bestTeamScore(List<int> scores, List<int> ages) {
30+
int n = scores.length;
31+
List<List<int>> players =
32+
List.filled(n, 0).map((e) => List.filled(2, 0)).toList();
33+
34+
for (int i = 0; i < n; i++) {
35+
players[i][0] = scores[i];
36+
players[i][1] = ages[i];
37+
}
38+
39+
// Sorting Players on the basis of scores(Ascending) and break their ties on the basis of age(Ascending)
40+
players.sort(
41+
(a, b) => a[0] == b[0] ? a[1].compareTo(b[1]) : a[0].compareTo(b[0]));
42+
43+
List<int> dp = List.filled(n, 0);
44+
int maxi = 0;
45+
for (int i = 0; i < n; i++) {
46+
dp[i] = search(dp, players, i) + players[i][0];
47+
maxi = max(maxi, dp[i]);
48+
}
49+
50+
return maxi;
51+
}
52+
53+
int search(List<int> dp, List<List<int>> players, int i) {
54+
int maxi = 0;
55+
for (int j = 0; j < i; j++) {
56+
if (dp[j] > maxi && players[j][1] <= players[i][1]) {
57+
maxi = dp[j];
58+
}
59+
}
60+
return maxi;
61+
}
62+
}
63+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
199199
- [**352.** Data Stream as Disjoint Intervals](DataStreamAsDisjointIntervals/data_stream_as_disjoint_intervals.dart)
200200
- [**460.** LFU Cache](LFUCache/lfu_cache.dart)
201201
- [**1137.** N-th Tribonacci Number](N-thTribonacciNumber/n_th_tribonacci_number.dart)
202+
- [**1626.** Best Team With No Conflicts](BestTeamWithNoConflicts/best_team_with_no_conflicts.dart)
202203

203204
## Reach me via
204205

0 commit comments

Comments
 (0)