-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
29 lines (29 loc) · 924 Bytes
/
Solution.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
class Solution {
public int maximumScore(int[] scores, int[][] edges) {
int n = scores.length;
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
for (int[] e : edges) {
int a = e[0], b = e[1];
g[a].add(b);
g[b].add(a);
}
for (int i = 0; i < n; ++i) {
g[i].sort((a, b) -> scores[b] - scores[a]);
g[i] = g[i].subList(0, Math.min(3, g[i].size()));
}
int ans = -1;
for (int[] e : edges) {
int a = e[0], b = e[1];
for (int c : g[a]) {
for (int d : g[b]) {
if (c != b && c != d && a != d) {
int t = scores[a] + scores[b] + scores[c] + scores[d];
ans = Math.max(ans, t);
}
}
}
}
return ans;
}
}