Skip to content

feat: add solutions to lc problem: No.3139 #4595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b8dec4a
feat: add solutions to lc problem: No.1960
samarthswami1016 Jul 20, 2025
4bcb5ed
Merge branch 'main' into main
samarthswami1016 Jul 20, 2025
ae77af9
Merge branch 'main' into main
samarthswami1016 Jul 20, 2025
6456b6a
style: format code and docs with prettier
samarthswami1016 Jul 20, 2025
0ed7b51
Merge branch 'main' into main
samarthswami1016 Jul 21, 2025
2f2b1da
Rename solution..java to Solution..java
samarthswami1016 Jul 22, 2025
3833ea4
Rename solution.py to Solution.py
samarthswami1016 Jul 22, 2025
9667fd4
Rename solution.cpp to Solution.cpp
samarthswami1016 Jul 22, 2025
213b18d
Rename solution.go to Solution.go
samarthswami1016 Jul 22, 2025
505a706
Rename Solution..java to Solution.java
samarthswami1016 Jul 22, 2025
1e3d066
Merge branch 'main' into main
samarthswami1016 Jul 22, 2025
d5c7d54
Update Solution.go
yanglbme Jul 22, 2025
f098f29
Update README.md
yanglbme Jul 22, 2025
54aa98d
Update README_EN.md
yanglbme Jul 22, 2025
c24de6c
Merge branch 'doocs:main' into main
samarthswami1016 Jul 24, 2025
b0a399e
feat: add solutions to lc problem: No.3139
samarthswami1016 Jul 25, 2025
a6d27ea
Merge branch 'main' into main
samarthswami1016 Jul 25, 2025
c6d8a35
style: format code and docs with prettier
samarthswami1016 Jul 25, 2025
6015d6f
Merge branch 'doocs:main' into main
samarthswami1016 Jul 29, 2025
221f56c
Update Solution.py
samarthswami1016 Jul 31, 2025
b6c5f3a
Update Solution.cpp
samarthswami1016 Jul 31, 2025
0a2f2fd
Update Solution.java
samarthswami1016 Jul 31, 2025
d80c509
Merge branch 'main' into main
samarthswami1016 Jul 31, 2025
bd1e1d9
Merge branch 'doocs:main' into main
samarthswami1016 Aug 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions solution/3100-3199/3139.Minimum Cost to Equalize Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,163 @@ tags:

```python

class Solution:
MOD = 10 ** 9 + 7

def solve(self, k):
sumDifferences = k * len(self.nums) - self.sumNums

ones = max(2 * (k - self.minNums) - sumDifferences, 0)
if (sumDifferences - ones) & 1 != 0:
ones += 1

return ones * self.cost1 + ((sumDifferences - ones) // 2) * self.cost2

def minCostToEqualizeArray(self, nums: List[int], cost1: int, cost2: int) -> int:
cost2 = min(2 * cost1, cost2)

self.nums = nums
self.minNums = min(nums)
self.sumNums = sum(nums)
self.cost1 = cost1
self.cost2 = cost2

m = max(nums)

sameParity = range(m, 10 ** 18, 2)
diffParity = range(m + 1, 10 ** 18, 2)
i = bisect_left(sameParity, 0, key=lambda i: self.solve(i + 2) - self.solve(i))
j = bisect_left(diffParity, 0, key=lambda j: self.solve(j + 2) - self.solve(j))

return min(self.solve(sameParity[i]), self.solve(diffParity[j])) % Solution.MOD

```

#### Java

```java

class Solution {
public int minCostToEqualizeArray(int[] A, int c1, int c2) {
int ma = A[0], mi = A[0], n = A.length, mod = 1000000007;
long total = 0;
for (int a: A) {
mi = Math.min(mi, a);
ma = Math.max(ma, a);
total += a;
}
total = 1l * ma * n - total;

if (c1 * 2 <= c2 || n <= 2) {
return (int) ((total * c1) % mod);
}

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

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

for (int i = 0; i < 2; i++) {
total += n;
res = Math.min(res, total % 2 * c1 + total / 2 * c2);
}
return (int) (res % mod);
}
}

```

#### C++

```cpp

class Solution {
public:
int minCostToEqualizeArray(vector<int>& A, int c1, int c2) {
int ma = *max_element(A.begin(), A.end());
int mi = *min_element(A.begin(), A.end());
int n = A.size(), mod = 1000000007;
long long su = accumulate(A.begin(), A.end(), 0LL);
long long total = 1LL * ma * n - su;

if (c1 * 2 <= c2 || n <= 2) {
return (total * c1) % mod;
}

long long op1 = max(0LL, (ma - mi) * 2 - total);
long long op2 = total - op1;
long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2;

total += op1 / (n - 2) * n;
op1 %= n - 2;
op2 = total - op1;
res = min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2);

for (int i = 0; i < 2; i++) {
total += n;
res = min(res, total % 2 * c1 + total / 2 * c2);
}

return res % mod;
}
};


```

#### Go

```go

func minCostToEqualizeArray(nums []int, cost1 int, cost2 int) int {
mx, mod := 0, int(1e9)+7
for _, v := range nums {
if v > mx {
mx = v
}
}
mv, cnt := 0, 0
for _, v := range nums {
d := mx - v
if cnt += d; d > mv {
mv = d
}
}
ans := find(mv, cnt, cost1, cost2)
if len(nums) <= 2 {
return ans % mod
}
for (mv<<1) > cnt {
mv, cnt = mv + 1, cnt + len(nums)
ans = min(ans, find(mv, cnt, cost1, cost2))
}
mv, cnt = mv + 1, cnt + len(nums)
ans = min(ans, find(mv, cnt, cost1, cost2))
return ans % mod
}

func find(mv, cnt, c1, c2 int) int {
switch {
case c1<<1 <= c2:
return cnt * c1
case mv<<1 <= cnt:
return (cnt>>1)*c2 + (cnt&1)*c1
}
return (cnt-mv)*c2 + (mv<<1-cnt)*c1
}

func min(x, y int) int {
if x < y {
return x
}
return y
}


```

<!-- tabs:end -->
Expand Down
139 changes: 139 additions & 0 deletions solution/3100-3199/3139.Minimum Cost to Equalize Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,163 @@ tags:

```python

class Solution:
MOD = 10 ** 9 + 7

def solve(self, k):
sumDifferences = k * len(self.nums) - self.sumNums

ones = max(2 * (k - self.minNums) - sumDifferences, 0)
if (sumDifferences - ones) & 1 != 0:
ones += 1

return ones * self.cost1 + ((sumDifferences - ones) // 2) * self.cost2

def minCostToEqualizeArray(self, nums: List[int], cost1: int, cost2: int) -> int:
cost2 = min(2 * cost1, cost2)

self.nums = nums
self.minNums = min(nums)
self.sumNums = sum(nums)
self.cost1 = cost1
self.cost2 = cost2

m = max(nums)

sameParity = range(m, 10 ** 18, 2)
diffParity = range(m + 1, 10 ** 18, 2)
i = bisect_left(sameParity, 0, key=lambda i: self.solve(i + 2) - self.solve(i))
j = bisect_left(diffParity, 0, key=lambda j: self.solve(j + 2) - self.solve(j))

return min(self.solve(sameParity[i]), self.solve(diffParity[j])) % Solution.MOD

```

#### Java

```java

class Solution {
public int minCostToEqualizeArray(int[] A, int c1, int c2) {
int ma = A[0], mi = A[0], n = A.length, mod = 1000000007;
long total = 0;
for (int a: A) {
mi = Math.min(mi, a);
ma = Math.max(ma, a);
total += a;
}
total = 1l * ma * n - total;

if (c1 * 2 <= c2 || n <= 2) {
return (int) ((total * c1) % mod);
}

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

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

for (int i = 0; i < 2; i++) {
total += n;
res = Math.min(res, total % 2 * c1 + total / 2 * c2);
}
return (int) (res % mod);
}
}

```

#### C++

```cpp

class Solution {
public:
int minCostToEqualizeArray(vector<int>& A, int c1, int c2) {
int ma = *max_element(A.begin(), A.end());
int mi = *min_element(A.begin(), A.end());
int n = A.size(), mod = 1000000007;
long long su = accumulate(A.begin(), A.end(), 0LL);
long long total = 1LL * ma * n - su;

if (c1 * 2 <= c2 || n <= 2) {
return (total * c1) % mod;
}

long long op1 = max(0LL, (ma - mi) * 2 - total);
long long op2 = total - op1;
long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2;

total += op1 / (n - 2) * n;
op1 %= n - 2;
op2 = total - op1;
res = min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2);

for (int i = 0; i < 2; i++) {
total += n;
res = min(res, total % 2 * c1 + total / 2 * c2);
}

return res % mod;
}
};


```

#### Go

```go

func minCostToEqualizeArray(nums []int, cost1 int, cost2 int) int {
mx, mod := 0, int(1e9)+7
for _, v := range nums {
if v > mx {
mx = v
}
}
mv, cnt := 0, 0
for _, v := range nums {
d := mx - v
if cnt += d; d > mv {
mv = d
}
}
ans := find(mv, cnt, cost1, cost2)
if len(nums) <= 2 {
return ans % mod
}
for (mv<<1) > cnt {
mv, cnt = mv + 1, cnt + len(nums)
ans = min(ans, find(mv, cnt, cost1, cost2))
}
mv, cnt = mv + 1, cnt + len(nums)
ans = min(ans, find(mv, cnt, cost1, cost2))
return ans % mod
}

func find(mv, cnt, c1, c2 int) int {
switch {
case c1<<1 <= c2:
return cnt * c1
case mv<<1 <= cnt:
return (cnt>>1)*c2 + (cnt&1)*c1
}
return (cnt-mv)*c2 + (mv<<1-cnt)*c1
}

func min(x, y int) int {
if x < y {
return x
}
return y
}


```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
int minCostToEqualizeArray(std::vector<int> &A, int c1, int c2) {
int ma = *std::max_element(A.begin(), A.end());
int mi = *std::min_element(A.begin(), A.end());
int n = A.size(), mod = 1000000007;
long long su = std::accumulate(A.begin(), A.end(), 0LL);
long long total = 1LL * ma * n - su;

if (c1 * 2 <= c2 || n <= 2) {
return (total * c1) % mod;
}

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

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

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

return res % mod;
}
};
Loading