@@ -20,28 +20,24 @@ public int minCostToEqualizeArray(int[] nums, int cost1, int cost2) {
20
20
sum += num ;
21
21
}
22
22
final int n = nums .length ;
23
- long total = ( max * ( long ) n ) - sum ;
23
+ long total = max * n - sum ;
24
24
// When operation one is always better:
25
25
if ((cost1 << 1 ) <= cost2 || n <= 2 ) {
26
- return (int ) (( total * ( long ) cost1 ) % LMOD );
26
+ return (int ) (total * cost1 % LMOD );
27
27
}
28
28
// When operation two is moderately better:
29
29
long op1 = Math .max (0L , ((max - min ) << 1L ) - total );
30
30
long op2 = total - op1 ;
31
- long result = (( op1 + (op2 & 1L )) * ( long ) cost1 ) + (( op2 >> 1L ) * ( long ) cost2 ) ;
31
+ long result = (op1 + (op2 & 1L )) * cost1 + (op2 >> 1L ) * cost2 ;
32
32
// When operation two is significantly better:
33
- total += op1 / (( long ) n - 2L ) * ( long ) n ;
34
- op1 %= (( long ) n - 2L ) ;
33
+ total += op1 / (n - 2L ) * n ;
34
+ op1 %= n - 2L ;
35
35
op2 = total - op1 ;
36
- result =
37
- Math .min (
38
- result , ((op1 + (op2 & 1L )) * (long ) cost1 ) + ((op2 >> 1L ) * (long ) cost2 ));
36
+ result = Math .min (result , (op1 + (op2 & 1L )) * cost1 + (op2 >> 1L ) * cost2 );
39
37
// When operation two is always better:
40
38
for (int i = 0 ; i < 2 ; ++i ) {
41
39
total += n ;
42
- result =
43
- Math .min (
44
- result , ((total & 1L ) * (long ) cost1 ) + ((total >> 1L ) * (long ) cost2 ));
40
+ result = Math .min (result , (total & 1L ) * cost1 + (total >> 1L ) * cost2 );
45
41
}
46
42
return (int ) (result % LMOD );
47
43
}
0 commit comments