@@ -10,26 +10,21 @@ public class Solution {
10
10
public int [] countPairs (int n , int [][] edges , int [] queries ) {
11
11
Map <Integer , Integer > edgeCount = new HashMap <>();
12
12
int [] degree = new int [n ];
13
-
14
13
for (int [] e : edges ) {
15
14
int u = e [0 ] - 1 ;
16
15
int v = e [1 ] - 1 ;
17
16
degree [u ]++;
18
17
degree [v ]++;
19
-
20
18
int eId = Math .min (u , v ) * n + Math .max (u , v );
21
19
edgeCount .put (eId , edgeCount .getOrDefault (eId , 0 ) + 1 );
22
20
}
23
-
24
21
Map <Integer , Integer > degreeCount = new HashMap <>();
25
22
int maxDegree = 0 ;
26
23
for (int d : degree ) {
27
24
degreeCount .put (d , degreeCount .getOrDefault (d , 0 ) + 1 );
28
25
maxDegree = Math .max (maxDegree , d );
29
26
}
30
-
31
27
int [] count = new int [2 * maxDegree + 1 ];
32
-
33
28
for (Map .Entry <Integer , Integer > d1 : degreeCount .entrySet ()) {
34
29
for (Map .Entry <Integer , Integer > d2 : degreeCount .entrySet ()) {
35
30
count [d1 .getKey () + d2 .getKey ()] +=
@@ -41,19 +36,15 @@ public int[] countPairs(int n, int[][] edges, int[] queries) {
41
36
for (int i = 0 ; i < count .length ; i ++) {
42
37
count [i ] /= 2 ;
43
38
}
44
-
45
39
for (Map .Entry <Integer , Integer > e : edgeCount .entrySet ()) {
46
40
int u = e .getKey () / n ;
47
41
int v = e .getKey () % n ;
48
-
49
42
count [degree [u ] + degree [v ]]--;
50
43
count [degree [u ] + degree [v ] - e .getValue ()]++;
51
44
}
52
-
53
45
for (int i = count .length - 2 ; i >= 0 ; i --) {
54
46
count [i ] += count [i + 1 ];
55
47
}
56
-
57
48
int [] res = new int [queries .length ];
58
49
for (int q = 0 ; q < queries .length ; q ++) {
59
50
res [q ] = ((queries [q ] + 1 ) >= count .length ) ? 0 : count [queries [q ] + 1 ];
0 commit comments