File tree 1 file changed +6
-5
lines changed
scripts/algorithms/I/Intervals Between Identical Elements
1 file changed +6
-5
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 544 ms (Top 6.59%) | Memory: 244.1 MB (Top 25.15%)
1
2
class Solution {
2
3
public long [] getDistances (int [] arr ) {
3
4
long [] output = new long [arr .length ];
@@ -8,12 +9,12 @@ public long[] getDistances(int[] arr) {
8
9
sumMap .put (arr [i ], 0l );
9
10
countMap .put (arr [i ], 0 );
10
11
}
11
-
12
+
12
13
output [i ] += i * (long )countMap .get (arr [i ]) - sumMap .get (arr [i ]);
13
14
sumMap .put (arr [i ], sumMap .get (arr [i ]) + i );
14
15
countMap .put (arr [i ], countMap .get (arr [i ]) + 1 );
15
16
}
16
-
17
+
17
18
sumMap = new HashMap <>();
18
19
countMap = new HashMap <>();
19
20
int len = arr .length ;
@@ -22,12 +23,12 @@ public long[] getDistances(int[] arr) {
22
23
sumMap .put (arr [i ], 0l );
23
24
countMap .put (arr [i ], 0 );
24
25
}
25
-
26
+
26
27
output [i ] += (len - i - 1 ) * (long )countMap .get (arr [i ]) - sumMap .get (arr [i ]);
27
28
sumMap .put (arr [i ], sumMap .get (arr [i ]) + (len - i - 1 ));
28
29
countMap .put (arr [i ], countMap .get (arr [i ]) + 1 );
29
30
}
30
-
31
+
31
32
return output ;
32
33
}
33
- }
34
+ }
You can’t perform that action at this time.
0 commit comments