leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 845e4ea5336e545433dd678e6f49d51c73ade322
parent 92313963434f48afcb16fb389e61537eeeaf3588
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 10 Apr 2024 19:17:47 +0200

1 Random Problem

Diffstat:
AProblems/2134.cpp | 20++++++++++++++++++++
MREADME.md | 1+
2 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/Problems/2134.cpp b/Problems/2134.cpp @@ -0,0 +1,20 @@ +class Solution { + public: + int minSwaps(const vector<int> &nums) const { + const int total = accumulate(begin(nums), end(nums), 0); + const int n = size(nums); + + if (total == n) return 0; + + int cnt = 0, res = INT_MAX; + for (int i = 0; i < total; i++) + cnt += nums[i]; + for (int j = 0, k = total; j < n; j++) { + res = min(res, total - cnt); + cnt += nums[k] - nums[j]; + if (++k == n) k = 0; + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -1011,6 +1011,7 @@ for solving problems. | 2125 | Medium | [Number of Laser Beams in a Bank](Problems/2125.cpp) | | 2130 | Medium | [Maximum Twin Sum of a Linked List](Problems/2130.cpp) | | 2131 | Medium | [Longest Palindrome by Concatenating Two Letter Words](Problems/2131.cpp) | +| 2134 | Medium | [Minimum Swaps to Group All 1's Together II](Problems/2134.cpp) | | 2140 | Medium | [Solving Questions With Brainpower](Problems/2140.cpp) | | 2141 | Hard | [Maximum Running Time of N Computers](Problems/2141.cpp) | | 2147 | Hard | [Number of Ways to Divide a Long Corridor](Problems/2147.cpp) |