Skip to content

Commit 2c0a20a

Browse files
Update Solution.java
1 parent f270bbc commit 2c0a20a

File tree

1 file changed

+25
-25
lines changed
  • solution/3100-3199/3139.Minimum Cost to Equalize Array

1 file changed

+25
-25
lines changed
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
class Solution {
2-
public int minCostToEqualizeArray(int[] A, int c1, int c2) {
3-
int ma = A[0], mi = A[0], n = A.length, mod = 1000000007;
4-
long total = 0;
5-
for (int a : A) {
6-
mi = Math.min(mi, a);
7-
ma = Math.max(ma, a);
8-
total += a;
9-
}
10-
total = 1L * ma * n - total;
2+
public int minCostToEqualizeArray(int[] A, int c1, int c2) {
3+
int ma = A[0], mi = A[0], n = A.length, mod = 1000000007;
4+
long total = 0;
5+
for (int a : A) {
6+
mi = Math.min(mi, a);
7+
ma = Math.max(ma, a);
8+
total += a;
9+
}
10+
total = 1L * ma * n - total;
1111

12-
if (c1 * 2 <= c2 || n <= 2) {
13-
return (int) ((total * c1) % mod);
14-
}
12+
if (c1 * 2 <= c2 || n <= 2) {
13+
return (int) ((total * c1) % mod);
14+
}
1515

16-
long op1 = Math.max(0L, (ma - mi) * 2L - total);
17-
long op2 = total - op1;
18-
long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2;
16+
long op1 = Math.max(0L, (ma - mi) * 2L - total);
17+
long op2 = total - op1;
18+
long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2;
1919

20-
total += (op1 / (n - 2)) * n;
21-
op1 %= (n - 2);
22-
op2 = total - op1;
23-
res = Math.min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2);
20+
total += (op1 / (n - 2)) * n;
21+
op1 %= (n - 2);
22+
op2 = total - op1;
23+
res = Math.min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2);
2424

25-
for (int i = 0; i < 2; i++) {
26-
total += n;
27-
res = Math.min(res, (total % 2) * c1 + (total / 2) * c2);
28-
}
25+
for (int i = 0; i < 2; i++) {
26+
total += n;
27+
res = Math.min(res, (total % 2) * c1 + (total / 2) * c2);
28+
}
2929

30-
return (int) (res % mod);
31-
}
30+
return (int) (res % mod);
31+
}
3232
}

0 commit comments

Comments
 (0)