Skip to content

Commit b6c5f3a

Browse files
Update Solution.cpp
1 parent 221f56c commit b6c5f3a

File tree

1 file changed

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

1 file changed

+22
-22
lines changed
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
class Solution {
22
public:
3-
int minCostToEqualizeArray(std::vector<int> &A, int c1, int c2) {
4-
int ma = *std::max_element(A.begin(), A.end());
5-
int mi = *std::min_element(A.begin(), A.end());
6-
int n = A.size(), mod = 1000000007;
7-
long long su = std::accumulate(A.begin(), A.end(), 0LL);
8-
long long total = 1LL * ma * n - su;
3+
int minCostToEqualizeArray(std::vector<int> &A, int c1, int c2) {
4+
int ma = *std::max_element(A.begin(), A.end());
5+
int mi = *std::min_element(A.begin(), A.end());
6+
int n = A.size(), mod = 1000000007;
7+
long long su = std::accumulate(A.begin(), A.end(), 0LL);
8+
long long total = 1LL * ma * n - su;
99

10-
if (c1 * 2 <= c2 || n <= 2) {
11-
return (total * c1) % mod;
12-
}
10+
if (c1 * 2 <= c2 || n <= 2) {
11+
return (total * c1) % mod;
12+
}
1313

14-
long long op1 = std::max(0LL, (ma - mi) * 2 - total);
15-
long long op2 = total - op1;
16-
long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2;
14+
long long op1 = std::max(0LL, (ma - mi) * 2 - total);
15+
long long op2 = total - op1;
16+
long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2;
1717

18-
total += op1 / (n - 2) * n;
19-
op1 %= n - 2;
20-
op2 = total - op1;
21-
res = std::min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2);
18+
total += op1 / (n - 2) * n;
19+
op1 %= n - 2;
20+
op2 = total - op1;
21+
res = std::min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2);
2222

23-
for (int i = 0; i < 2; i++) {
24-
total += n;
25-
res = std::min(res, total % 2 * c1 + total / 2 * c2);
26-
}
23+
for (int i = 0; i < 2; i++) {
24+
total += n;
25+
res = std::min(res, total % 2 * c1 + total / 2 * c2);
26+
}
2727

28-
return res % mod;
29-
}
28+
return res % mod;
29+
}
3030
};

0 commit comments

Comments
 (0)