-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBestTeamWithNoConflicts.java
48 lines (40 loc) · 1.6 KB
/
BestTeamWithNoConflicts.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
class Solution {
public int bestTeamScore(int[] scores, int[] ages) {
int N = ages.length;
int[][] ageScorePair = new int[N][2];
for (int i = 0; i < N; i++) {
ageScorePair[i][0] = scores[i];
ageScorePair[i][1] = ages[i];
}
// Sort in ascending order of score and then by age.
Arrays.sort(ageScorePair, (a,b) -> a[0] == b[0] ? a[1]-b[1] : a[0]-b[0]);
int highestAge = 0;
for (int i : ages) {
highestAge = Math.max(highestAge, i);
}
int[] BIT = new int[highestAge + 1];
int answer = Integer.MIN_VALUE;
for (int[] ageScore : ageScorePair) {
// Maximum score up to this age might not have all the players of this age.
int currentBest = ageScore[0] + queryBIT(BIT, ageScore[1]);
// Update the tree with the current age and its best score.
updateBIT(BIT, ageScore[1], currentBest);
answer = Math.max(answer, currentBest);
}
return answer;
}
// Query tree to get the maximum score up to this age.
private int queryBIT(int[] BIT, int age) {
int maxScore = Integer.MIN_VALUE;
for (int i = age; i > 0; i -= i & (-i)) {
maxScore = Math.max(maxScore, BIT[i]);
}
return maxScore;
}
// Update the maximum score for all the nodes with an age greater than the given age.
private void updateBIT(int[] BIT, int age, int currentBest) {
for (int i = age; i < BIT.length; i += i & (-i)) {
BIT[i] = Math.max(BIT[i], currentBest);
}
}
}