-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Closed
Closed
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
b8dec4a
feat: add solutions to lc problem: No.1960
samarthswami1016 4bcb5ed
Merge branch 'main' into main
samarthswami1016 ae77af9
Merge branch 'main' into main
samarthswami1016 6456b6a
style: format code and docs with prettier
samarthswami1016 0ed7b51
Merge branch 'main' into main
samarthswami1016 2f2b1da
Rename solution..java to Solution..java
samarthswami1016 3833ea4
Rename solution.py to Solution.py
samarthswami1016 9667fd4
Rename solution.cpp to Solution.cpp
samarthswami1016 213b18d
Rename solution.go to Solution.go
samarthswami1016 505a706
Rename Solution..java to Solution.java
samarthswami1016 1e3d066
Merge branch 'main' into main
samarthswami1016 d5c7d54
Update Solution.go
yanglbme f098f29
Update README.md
yanglbme 54aa98d
Update README_EN.md
yanglbme c24de6c
Merge branch 'doocs:main' into main
samarthswami1016 b0a399e
feat: add solutions to lc problem: No.3139
samarthswami1016 a6d27ea
Merge branch 'main' into main
samarthswami1016 c6d8a35
style: format code and docs with prettier
samarthswami1016 6015d6f
Merge branch 'doocs:main' into main
samarthswami1016 221f56c
Update Solution.py
samarthswami1016 b6c5f3a
Update Solution.cpp
samarthswami1016 0a2f2fd
Update Solution.java
samarthswami1016 d80c509
Merge branch 'main' into main
samarthswami1016 bd1e1d9
Merge branch 'doocs:main' into main
samarthswami1016 312c25f
Merge branch 'main' into main
samarthswami1016 f270bbc
Update Solution.cpp
samarthswami1016 2c0a20a
Update Solution.java
samarthswami1016 d536b1e
Update Solution.py
samarthswami1016 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
solution/3100-3199/3139.Minimum Cost to Equalize Array/Solution.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Solution { | ||
public: | ||
int minCostToEqualizeArray(std::vector<int>& A, int c1, int c2) { | ||
const int mod = 1'000'000'007; | ||
const int n = static_cast<int>(A.size()); | ||
const int ma = *std::max_element(A.begin(), A.end()); | ||
const int mi = *std::min_element(A.begin(), A.end()); | ||
const long long su = std::accumulate(A.begin(), A.end(), 0LL); | ||
|
||
long long total = 1LL * ma * n - su; | ||
|
||
if (c1 * 2 <= c2 || n <= 2) { | ||
return static_cast<int>(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 static_cast<int>(res % mod); | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.