forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1626-best-team-with-no-conflicts.c
37 lines (33 loc) · 1.04 KB
/
1626-best-team-with-no-conflicts.c
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
struct Player {
int age;
int score;
};
int comparePlayers(const void *a, const void *b) {
struct Player *playerA = (struct Player *)a;
struct Player *playerB = (struct Player *)b;
if (playerA->age != playerB->age) {
return playerA->age - playerB->age;
} else {
return playerA->score - playerB->score;
}
}
int bestTeamScore(int* scores, int scoresSize, int* ages, int agesSize) {
struct Player players[scoresSize];
for (int i = 0; i < scoresSize; i++) {
players[i].age = ages[i];
players[i].score = scores[i];
}
qsort(players, scoresSize, sizeof(struct Player), comparePlayers);
int dp[scoresSize];
int ans = 0;
for (int i = 0; i < scoresSize; i++) {
dp[i] = players[i].score;
for (int j = 0; j < i; j++) {
if (players[j].score <= players[i].score) {
dp[i] = (dp[i] > dp[j] + players[i].score) ? dp[i] : dp[j] + players[i].score;
}
}
ans = (ans > dp[i]) ? ans : dp[i];
}
return ans;
}