Problem Statement:
Given a binary array data, return the minimum number of swaps required to group all 1’s present in the array together in any place in the array.
Example 1:
Input: [1,0,1,0,1]
Output: 1
Explanation:
There are 3 ways to group all 1's together:
[1,1,1,0,0] using 1 swap.
[0,1,1,1,0] using 2 swaps.
[0,0,1,1,1] using 1 swap.
The minimum is 1.
Example 2:
Input: [0,0,0,1,0]
Output: 0
Explanation:
Since there is only one 1 in the array, no swaps needed.
Example 3:
Input: [1,0,1,0,1,0,0,1,1,0,1]
Output: 3
Explanation:
One possible solution that uses 3 swaps is [0,0,0,0,0,1,1,1,1,1,1].
This is my solution. I used sliding window.
First I counted total number of ones in the given array.
To group the 1s in minimum number of swaps we have to group them up in a cluster where their density is already more.
So I made an array which keeps a count of 1s encountered till that element
Then I created a window of the size of total 1s in that array and moved that window till the end to find which sub array had maximum number of 1s, because this is the area where we will insert other 1s
The code is as follows
int minSwaps(vector<int>& data) {
// Size of given array
int size = data.size();
// Total ones in given array
int totalOnes = 0;
// Counting total ones in array
for(int i = 0 ; i < size;++i){
if(data[i]==1) totalOnes++;
}
if(totalOnes==size) return 0;
if(totalOnes==0) return 0;
// Using a winow of the size of total ones
int window = totalOnes;
// maxOnes will store the value of maximum ones in any sliding window of size 'window'
int maxOnes = INT_MIN;
// This array will store total number of ones till index i
vector<int> onesTillNow(size,0);
if(data[0]==1)
onesTillNow[0]=1;
else
onesTillNow[0]=0;
for(int i = 1; i < size;++i){
if(data[i]==1)
onesTillNow[i] = onesTillNow[i-1] + 1;
else
onesTillNow[i] = onesTillNow[i-1];
}
for(int i = window - 1; i < size ; ++i){
if(i == window - 1 )
totalOnes = onesTillNow[i];
else
totalOnes = onesTillNow[i] - onesTillNow[i-window];
if(totalOnes > maxOnes) maxOnes = totalOnes;
}
return window - maxOnes;
}
Can you suggest any improvements to this or a better way to do it?