-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathques1.cpp
More file actions
41 lines (33 loc) · 720 Bytes
/
Copy pathques1.cpp
File metadata and controls
41 lines (33 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int minSwaps(vector<int> &nums)
{
int n = nums.size();
int ones = accumulate(nums.begin(), nums.end(), 0);
vector<int> vec(2 * n);
for (int i = 0; i < 2 * n; i++)
{
vec[i] = nums[i % n];
}
int i = 0;
int j = 0;
int res = INT_MAX;
for (int i = 0, count = 0; i < n; i++)
{
while (j - i < ones)
{
count += nums[j];
j++;
}
res = min(res, ones - count);
count -= nums[i];
}
return res;
}
int main(int argc, char const *argv[])
{
vector<int> nums = {1, 0, 1, 0, 1, 1, 0};
cout << minSwaps(nums) << endl;
return 0;
}