Skip to content

Commit f270bbc

Browse files
Update Solution.cpp
1 parent 312c25f commit f270bbc

File tree

1 file changed

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

1 file changed

+13
-12
lines changed
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
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);
3+
int minCostToEqualizeArray(std::vector<int>& A, int c1, int c2) {
4+
const int mod = 1'000'000'007;
5+
const int n = static_cast<int>(A.size());
6+
const int ma = *std::max_element(A.begin(), A.end());
7+
const int mi = *std::min_element(A.begin(), A.end());
8+
const long long su = std::accumulate(A.begin(), A.end(), 0LL);
9+
810
long long total = 1LL * ma * n - su;
911

1012
if (c1 * 2 <= c2 || n <= 2) {
11-
return (total * c1) % mod;
13+
return static_cast<int>(total * c1 % mod);
1214
}
1315

1416
long long op1 = std::max(0LL, (ma - mi) * 2 - total);
1517
long long op2 = total - op1;
1618
long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2;
1719

1820
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);
21+
op1 %= (n - 2);
22+
op2 = total - op1;
23+
res = std::min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2);
2224

23-
for (int i = 0; i < 2; i++) {
25+
for (int i = 0; i < 2; ++i) {
2426
total += n;
2527
res = std::min(res, total % 2 * c1 + total / 2 * c2);
2628
}
27-
28-
return res % mod;
29+
return static_cast<int>(res % mod);
2930
}
3031
};

0 commit comments

Comments
 (0)