diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /min_swap_group_1.cpp | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'min_swap_group_1.cpp')
-rw-r--r-- | min_swap_group_1.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/min_swap_group_1.cpp b/min_swap_group_1.cpp new file mode 100644 index 0000000..89ab5dd --- /dev/null +++ b/min_swap_group_1.cpp @@ -0,0 +1,40 @@ +// 2134. Minimum Swaps to Group All 1's Together II +#include "leetcode.h" + +/* + * a swap is defined as taking two distinct position in an array and swapping + * the values in them. a circular array is defined as an array where we consider + * the first element and the last element to adjacent. given a binary circular + * array 'nums', return the minimum number of swaps required to group all 1's + * preent in the array together at any location. + */ + +class Solution { +public: + int minSwaps(vector<int> &nums) { + int n = nums.size(), k = 0; + for (int i = 0; i < n; i++) + k += (nums[i] == 1); + nums.insert(nums.end(), nums.begin(), nums.end()); + int ans = n, i = 0, j = 0, l = 0; + while (j < 2 * n) { + l += (nums[j] == 1); + if (j - i + 1 == k) { + ans = min(ans, k - l); + l -= (nums[i] == 1); + i++; + } + j++; + } + return ans == n ? 0 : ans; + } +}; + +int main() { + Solution obj; + vector<int> n1 = {0, 1, 0, 1, 1, 0, 0}, n2 = {0, 1, 1, 1, 0, 0, 1, 1, 0}, + n3 = {1, 1, 0, 0, 1}; + printf("%d\n", obj.minSwaps(n1)); // expect: 1 + printf("%d\n", obj.minSwaps(n2)); // expect: 2 + printf("%d\n", obj.minSwaps(n3)); // expect: 0 +} |