Leetcode 1151. Minimum Swaps to Group All 1's Together.
class Solution {
public int minSwaps(int[] data) {
//Sliding window
int count = 0;
for(int i=0; i<data.length; i++){
if(data[i]==1) count++;
}
int cur = 0, maxCount = 0;
for(int i=0; i<data.length; i++){
if(i>=count){
cur -= data[i-count];
}
cur += data[i];
maxCount = Math.max(maxCount,cur);
}
return count-maxCount;
}
}