📌  相关文章
📜  将所有 1 组合在一起所需的最小掉期

📅  最后修改于: 2022-05-13 01:57:52.531000             🧑  作者: Mango

将所有 1 组合在一起所需的最小掉期

给定一个由 0 和 1 组成的数组,我们需要编写一个程序来找到将数组中存在的所有 1 组合在一起所需的最小交换次数。

例子:

Input : arr[] = {1, 0, 1, 0, 1}
Output : 1
Explanation: Only 1 swap is required to 
group all 1's together. Swapping index 1
and 4 will give arr[] = {1, 1, 1, 0, 0}

Input : arr[] = {1, 0, 1, 0, 1, 1}
Output : 1

一个简单的解决方案是首先计算数组中 1 的总数。假设这个计数是 x,现在我们需要找到这个数组的长度为 x 的子数组,其中 1 的个数最多。所需的最小交换将是长度为 x 的子数组中 0 的数量,最大数量为 1。
时间复杂度:O(n 2 )

一个有效的解决方案是使用滑动窗口技术的概念来优化上述方法中寻找子阵列的蛮力技术。我们可以维护一个preCount数组,以 O(1) 的时间复杂度查找子数组中存在的 1 的数量。

下面是上述想法的实现:

C++
// C++ program to find minimum swaps
// required to group all 1's together
#include 
#include 
 
using namespace std;
 
// Function to find minimum swaps
// to group all 1's together
int minSwaps(int arr[], int n) {
 
  int noOfOnes = 0;
 
  // find total number of all in the array
  for (int i = 0; i < n; i++) {
    if (arr[i] == 1)
      noOfOnes++;
  }
 
  // length of subarray to check for
  int x = noOfOnes;
 
  int maxOnes = INT_MIN;
 
  // array to store number of 1's upto
  // ith index
  int preCompute[n] = {0};
 
  // calculate number of 1's upto ith
  // index and store in the array preCompute[]
  if (arr[0] == 1)
    preCompute[0] = 1;
  for (int i = 1; i < n; i++) {
    if (arr[i] == 1) {
      preCompute[i] = preCompute[i - 1] + 1;
    } else
      preCompute[i] = preCompute[i - 1];
  }
 
  // using sliding window technique to find
  // max number of ones in subarray of length x
  for (int i = x - 1; i < n; i++) {
    if (i == (x - 1))
      noOfOnes = preCompute[i];
    else
      noOfOnes = preCompute[i] - preCompute[i - x];
     
    if (maxOnes < noOfOnes)
      maxOnes = noOfOnes;
  }
 
  // calculate number of zeros in subarray
  // of length x with maximum number of 1's
  int noOfZeroes = x - maxOnes;
 
  return noOfZeroes;
}
 
// Driver Code
int main() {
  int a[] = {1, 0, 1, 0, 1};
  int n = sizeof(a) / sizeof(a[0]);
  cout << minSwaps(a, n);
  return 0;
}


Java
// Java  program to find minimum swaps
// required to group all 1's together
 
import java.io.*;
 
class GFG {
 
// Function to find minimum swaps
// to group all 1's together
 static int minSwaps(int arr[], int n) {
 
int noOfOnes = 0;
 
// find total number of all in the array
for (int i = 0; i < n; i++) {
    if (arr[i] == 1)
    noOfOnes++;
}
 
// length of subarray to check for
int x = noOfOnes;
 
int maxOnes = Integer.MIN_VALUE;
 
// array to store number of 1's upto
// ith index
int preCompute[] = new int[n];
 
// calculate number of 1's upto ith
// index and store in the array preCompute[]
if (arr[0] == 1)
    preCompute[0] = 1;
for (int i = 1; i < n; i++) {
    if (arr[i] == 1) {
    preCompute[i] = preCompute[i - 1] + 1;
    } else
    preCompute[i] = preCompute[i - 1];
}
 
// using sliding window technique to find
// max number of ones in subarray of length x
for (int i = x - 1; i < n; i++) {
    if (i == (x - 1))
    noOfOnes = preCompute[i];
    else
    noOfOnes = preCompute[i] - preCompute[i - x];
     
    if (maxOnes < noOfOnes)
    maxOnes = noOfOnes;
}
 
// calculate number of zeros in subarray
// of length x with maximum number of 1's
int noOfZeroes = x - maxOnes;
 
return noOfZeroes;
}
 
// Driver Code
public static void main (String[] args) {
int a[] = {1, 0, 1, 0, 1};
int n = a.length;
System.out.println( minSwaps(a, n));
     
    }
}
 
// This code is contributed by vt_m.


Python3
# Python program to
# find minimum swaps
# required to group
# all 1's together
 
# Function to find minimum swaps
# to group all 1's together
def minSwaps(arr,n):
     
    noOfOnes = 0
  
    # find total number of
    # all in the array
    for i in range(n):
        if (arr[i] == 1):
            noOfOnes=noOfOnes+1
   
  
    # length of subarray to check for
    x = noOfOnes
  
    maxOnes = -2147483648
  
    # array to store number of 1's upto
    # ith index
    preCompute={}
  
    # calculate number of 1's upto ith
    # index and store in the
    # array preCompute[]
    if (arr[0] == 1):
        preCompute[0] = 1
    for i in range(1,n):
        if (arr[i] == 1):
            preCompute[i] = preCompute[i - 1] + 1
        else:
            preCompute[i] = preCompute[i - 1]
   
  
    # using sliding window
    # technique to find
    # max number of ones in
    # subarray of length x
    for i in range(x-1,n):
        if (i == (x - 1)):
            noOfOnes = preCompute[i]
        else:
            noOfOnes = preCompute[i] - preCompute[i - x]
      
        if (maxOnes < noOfOnes):
            maxOnes = noOfOnes
   
  
    # calculate number of zeros in subarray
    # of length x with maximum number of 1's
    noOfZeroes = x - maxOnes
  
    return noOfZeroes
   
# Driver code
 
a = [1, 0, 1, 0, 1]
n = len(a)
 
print(minSwaps(a, n))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to find minimum swaps
// required to group all 1's together
 
using System;
 
class GFG {
 
    // Function to find minimum swaps
    // to group all 1's together
    static int minSwaps(int []arr, int n) {
     
        int noOfOnes = 0;
         
        // find total number of all in the array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1)
            noOfOnes++;
        }
         
        // length of subarray to check for
        int x = noOfOnes;
         
        int maxOnes = int.MinValue;
         
        // array to store number of 1's upto
        // ith index
        int []preCompute = new int[n];
         
        // calculate number of 1's upto ith
        // index and store in the array preCompute[]
        if (arr[0] == 1)
            preCompute[0] = 1;
        for (int i = 1; i < n; i++) {
            if (arr[i] == 1) {
            preCompute[i] = preCompute[i - 1] + 1;
            } else
            preCompute[i] = preCompute[i - 1];
        }
         
        // using sliding window technique to find
        // max number of ones in subarray of length x
        for (int i = x - 1; i < n; i++) {
            if (i == (x - 1))
            noOfOnes = preCompute[i];
            else
            noOfOnes = preCompute[i] - preCompute[i - x];
             
            if (maxOnes < noOfOnes)
            maxOnes = noOfOnes;
        }
         
        // calculate number of zeros in subarray
        // of length x with maximum number of 1's
        int noOfZeroes = x - maxOnes;
         
        return noOfZeroes;
    }
     
    // Driver Code
    public static void Main ()
    {
        int []a = {1, 0, 1, 0, 1};
        int n = a.Length;
        Console.WriteLine( minSwaps(a, n));
         
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// C++ code for minimum swaps
// required to group all 1's together
#include 
#include 
 
using namespace std;
 
// Function to find minimum swaps
// to group all 1's together
int minSwaps(int arr[], int n)
{
 
int numberOfOnes = 0;
 
// find total number of all 1's in the array
for (int i = 0; i < n; i++) {
    if (arr[i] == 1)
    numberOfOnes++;
}
 
// length of subarray to check for
int x = numberOfOnes;
 
int count_ones = 0, maxOnes;
 
// Find 1's for first subarray of length x
for(int i = 0; i < x; i++){
    if(arr[i] == 1)
    count_ones++;
}
     
maxOnes = count_ones;
     
// using sliding window technique to find
// max number of ones in subarray of length x
for (int i = 1; i <= n-x; i++) {
     
    // first remove leading element and check
    // if it is equal to 1 then decrement the
    // value of count_ones by 1
    if (arr[i-1] == 1)
    count_ones--;
     
    // Now add trailing element and check
    // if it is equal to 1 Then increment
    // the value of count_ones by 1
    if(arr[i+x-1] == 1)
    count_ones++;
     
    if (maxOnes < count_ones)
    maxOnes = count_ones;
}
 
// calculate number of zeros in subarray
// of length x with maximum number of 1's
int numberOfZeroes = x - maxOnes;
 
return numberOfZeroes;
}
 
// Driver Code
int main() {
     
int a[] = {0, 0, 1, 0, 1, 1, 0, 0, 1};
int n = sizeof(a) / sizeof(a[0]);
 
cout << minSwaps(a, n);
 
return 0;
 
}


Java
// java program to find largest number
// smaller than equal to n with m set
// bits then m-1 0 bits.
public class GFG {
     
    // Function to find minimum swaps
    // to group all 1's together
    static int minSwaps(int arr[], int n)
    {
     
        int numberOfOnes = 0;
         
        // find total number of all 1's
        // in the array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1)
                numberOfOnes++;
        }
         
        // length of subarray to check for
        int x = numberOfOnes;
         
        int count_ones = 0, maxOnes;
         
        // Find 1's for first subarray
        // of length x
        for(int i = 0; i < x; i++){
            if(arr[i] == 1)
                count_ones++;
        }
             
        maxOnes = count_ones;
             
        // using sliding window technique
        // to find max number of ones in
        // subarray of length x
        for (int i = 1; i <= n-x; i++) {
             
            // first remove leading element
            // and check if it is equal to
            // 1 then decrement the
            // value of count_ones by 1
            if (arr[i - 1] == 1)
                count_ones--;
             
            // Now add trailing element
            // and check if it is equal
            // to 1 Then increment the
            // value of count_ones by 1
            if(arr[i + x - 1] == 1)
                count_ones++;
             
            if (maxOnes < count_ones)
            maxOnes = count_ones;
        }
         
        // calculate number of zeros in
        // subarray of length x with
        // maximum number of 1's
        int numberOfZeroes = x - maxOnes;
         
        return numberOfZeroes;
    }
     
    // Driver code
    public static void main(String args[])
    {
        int a[] = new int[]{0, 0, 1, 0,
                            1, 1, 0, 0, 1};
        int n = a.length;
         
        System.out.println(minSwaps(a, n));
    }
}
 
// This code is contributed by Sam007


Python3
# Python code for minimum
# swaps required to group
# all 1's together
 
# Function to find minimum
# swaps to group all 1's
# together
def minSwaps(arr, n) :
 
    numberOfOnes = 0
     
    # find total number of
    # all 1's in the array
    for i in range(0, n) :
 
        if (arr[i] == 1) :
            numberOfOnes = numberOfOnes + 1
     
    # length of subarray
    # to check for
    x = numberOfOnes
     
    count_ones = 0
    maxOnes = 0
     
    # Find 1's for first
    # subarray of length x
    for i in range(0, x) :
 
        if(arr[i] == 1) :
            count_ones = count_ones + 1
         
    maxOnes = count_ones
         
    # using sliding window
    # technique to find
    # max number of ones in
    # subarray of length x
    for i in range(1, (n - x + 1)) :
         
        # first remove leading
        # element and check
        # if it is equal to 1
        # then decrement the
        # value of count_ones by 1
        if (arr[i - 1] == 1) :
            count_ones = count_ones - 1
         
        # Now add trailing
        # element and check
        # if it is equal to 1
        # Then increment
        # the value of count_ones by 1
        if(arr[i + x - 1] == 1) :
            count_ones = count_ones + 1
         
        if (maxOnes < count_ones) :
                maxOnes = count_ones
     
    # calculate number of
    # zeros in subarray
    # of length x with
    # maximum number of 1's
    numberOfZeroes = x - maxOnes
     
    return numberOfZeroes
 
# Driver Code
a = [0, 0, 1, 0, 1, 1, 0, 0, 1]
n = 9
print (minSwaps(a, n))
 
# This code is contributed
# by Manish Shaw(manishshaw1)


C#
// C# code for minimum swaps
// required to group all 1's together
using System;
 
class GFG{
     
    // Function to find minimum swaps
    // to group all 1's together
    static int minSwaps(int []arr, int n)
    {
     
        int numberOfOnes = 0;
         
        // find total number of all 1's in the array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1)
            numberOfOnes++;
        }
         
        // length of subarray to check for
        int x = numberOfOnes;
         
        int count_ones = 0, maxOnes;
         
        // Find 1's for first subarray of length x
        for(int i = 0; i < x; i++){
            if(arr[i] == 1)
            count_ones++;
        }
             
        maxOnes = count_ones;
             
        // using sliding window technique to find
        // max number of ones in subarray of length x
        for (int i = 1; i <= n-x; i++) {
             
            // first remove leading element and check
            // if it is equal to 1 then decrement the
            // value of count_ones by 1
            if (arr[i - 1] == 1)
            count_ones--;
             
            // Now add trailing element and check
            // if it is equal to 1 Then increment
            // the value of count_ones by 1
            if(arr[i + x - 1] == 1)
            count_ones++;
             
            if (maxOnes < count_ones)
            maxOnes = count_ones;
        }
         
        // calculate number of zeros in subarray
        // of length x with maximum number of 1's
        int numberOfZeroes = x - maxOnes;
         
        return numberOfZeroes;
    }
     
    // Driver Code
    static public void Main ()
    {
        int []a = {0, 0, 1, 0, 1, 1, 0, 0, 1};
        int n = a.Length;
     
        Console.WriteLine(minSwaps(a, n));
         
    }
}
// This code is contributed by vt_m.


PHP


Javascript


C++
#include 
 
using namespace std;
 
int minSwaps(int arr[], int n)
{
  int totalCount = 0; // To store total number of ones
  // count total no of ones
  for (int i = 0; i < n; i++)
    totalCount += arr[i];
 
  int currCount = 0; // To store count of ones in current window
  int maxCount = 0;  // To store maximum count ones out of all windows
  int i = 0;         // start of window
  int j = 0;         // end of window
 
  while (j < n)
  {
    currCount += arr[j];
 
    // update maxCount when reach window size i.e. total count of ones in array
    if ((j - i + 1) == totalCount)
    {
      maxCount = max(maxCount, currCount);
      if (arr[i] == 1)
        currCount--; // decrease current count if first element of window is 1
      i++;           // slide window
    }
    j++;
  }
 
  return totalCount - maxCount; // return total no of ones in array - maximum count of ones out of all windows
}
 
// Driver Code
int main()
{
 
  int a[] = {1, 0, 1, 0, 1, 1};
  int n = sizeof(a) / sizeof(a[0]);
 
  cout << minSwaps(a, n);
 
  return 0;
}


Java
import java.io.*;
import java.util.*;
 
class GFG{
     
static int minSwaps(int[] arr, int n)
{
     
    // To store total number of ones
    int totalCount = 0;
     
    // Count total no of ones
    int i;
    for(i = 0; i < n; i++)
        totalCount += arr[i];
 
    int currCount = 0; // To store count of ones in current window
    int maxCount = 0;  // To store maximum count ones out
                       // of all windows
                       
    // start of window
    i = 0;
     
    // end of window
    int j = 0;
 
    while (j < n)
    {
        currCount += arr[j];
 
        // update maxCount when reach window size i.e.
        // total count of ones in array
        if ((j - i + 1) == totalCount)
        {
            maxCount = Math.max(maxCount, currCount);
            if (arr[i] == 1)
                currCount--; // decrease current count
                             // if first element of
                             // window is 1
                              
            // slide window
            i++;
        }
        j++;
    }
 
    return totalCount - maxCount; // return total no of ones in array
                                  // - maximum count of ones out of
                                  // all windows
}
 
// Driver Code
public static void main(String args[])
    {
    int[] a = { 1, 0, 1, 0, 1, 1 };
    int n = a.length;
 
    System.out.println(minSwaps(a, n));
}
}
 
// This code is contributed by shivanisinghss2110


Python
def minSwaps(arr, n):
   
    # To store total number of ones
    totalCount = 0
     
    # count total no of ones
    for i in range(0,n):
        totalCount += arr[i]
         
    currCount = 0 # To store count of ones in current window
    maxCount = 0  # To store maximum count ones out of all windows
    i = 0         # start of window
    j = 0         # end of window
     
    while (j < n):
        currCount += arr[j]
         
        # update maxCount when reach window size i.e. total count of ones in array
        if ((j - i + 1) == totalCount):
            maxCount = max(maxCount, currCount)
            if (arr[i] == 1):
                currCount -= 1
                 
                # decrease current count if first element of window is 1
            i += 1           # slide window
        j += 1
     
    return totalCount - maxCount # return total no of ones in array - maximum count of ones out of all windows
 
# Driver Code
a = [1, 0, 1, 0, 1, 1]
n = len(a)
print(minSwaps(a, n))
 
# this code is contributed by shivanisighss2110


C#
using System;
 
class GFG{
     
static int minSwaps(int[] arr, int n)
{
     
    // To store total number of ones
    int totalCount = 0;
     
    // Count total no of ones
    int i;
    for(i = 0; i < n; i++)
        totalCount += arr[i];
 
    int currCount = 0; // To store count of ones in current window
    int maxCount = 0;  // To store maximum count ones out
                       // of all windows
                       
    // start of window
    i = 0;
     
    // end of window
    int j = 0;
 
    while (j < n)
    {
        currCount += arr[j];
 
        // update maxCount when reach window size i.e.
        // total count of ones in array
        if ((j - i + 1) == totalCount)
        {
            maxCount = Math.Max(maxCount, currCount);
            if (arr[i] == 1)
                currCount--; // decrease current count
                             // if first element of
                             // window is 1
                              
            // slide window
            i++;
        }
        j++;
    }
 
    return totalCount - maxCount; // return total no of ones in array
                                  // - maximum count of ones out of
                                  // all windows
}
 
// Driver Code
public static void Main()
{
    int[] a = { 1, 0, 1, 0, 1, 1 };
    int n = a.Length;
 
    Console.WriteLine(minSwaps(a, n));
}
}
 
// This code is contributed by ukasp


Javascript


输出
1

时间复杂度:O(n)
辅助空间:O(n)

另一种有效的方法:
首先计算数组中 1 的总数。假设这个计数是 x,现在使用窗口滑动技术的概念找到这个数组的长度为 x 的子数组,其中 1 的数量最多。维护一个变量以在 O(1) 额外空间中查找子数组中存在的 1 的数量,并为每个子数组维护 maxOnes 变量,最后返回 numberOfZeros (numberOfZeroes = x – maxOnes)。

C++

// C++ code for minimum swaps
// required to group all 1's together
#include 
#include 
 
using namespace std;
 
// Function to find minimum swaps
// to group all 1's together
int minSwaps(int arr[], int n)
{
 
int numberOfOnes = 0;
 
// find total number of all 1's in the array
for (int i = 0; i < n; i++) {
    if (arr[i] == 1)
    numberOfOnes++;
}
 
// length of subarray to check for
int x = numberOfOnes;
 
int count_ones = 0, maxOnes;
 
// Find 1's for first subarray of length x
for(int i = 0; i < x; i++){
    if(arr[i] == 1)
    count_ones++;
}
     
maxOnes = count_ones;
     
// using sliding window technique to find
// max number of ones in subarray of length x
for (int i = 1; i <= n-x; i++) {
     
    // first remove leading element and check
    // if it is equal to 1 then decrement the
    // value of count_ones by 1
    if (arr[i-1] == 1)
    count_ones--;
     
    // Now add trailing element and check
    // if it is equal to 1 Then increment
    // the value of count_ones by 1
    if(arr[i+x-1] == 1)
    count_ones++;
     
    if (maxOnes < count_ones)
    maxOnes = count_ones;
}
 
// calculate number of zeros in subarray
// of length x with maximum number of 1's
int numberOfZeroes = x - maxOnes;
 
return numberOfZeroes;
}
 
// Driver Code
int main() {
     
int a[] = {0, 0, 1, 0, 1, 1, 0, 0, 1};
int n = sizeof(a) / sizeof(a[0]);
 
cout << minSwaps(a, n);
 
return 0;
 
}

Java

// java program to find largest number
// smaller than equal to n with m set
// bits then m-1 0 bits.
public class GFG {
     
    // Function to find minimum swaps
    // to group all 1's together
    static int minSwaps(int arr[], int n)
    {
     
        int numberOfOnes = 0;
         
        // find total number of all 1's
        // in the array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1)
                numberOfOnes++;
        }
         
        // length of subarray to check for
        int x = numberOfOnes;
         
        int count_ones = 0, maxOnes;
         
        // Find 1's for first subarray
        // of length x
        for(int i = 0; i < x; i++){
            if(arr[i] == 1)
                count_ones++;
        }
             
        maxOnes = count_ones;
             
        // using sliding window technique
        // to find max number of ones in
        // subarray of length x
        for (int i = 1; i <= n-x; i++) {
             
            // first remove leading element
            // and check if it is equal to
            // 1 then decrement the
            // value of count_ones by 1
            if (arr[i - 1] == 1)
                count_ones--;
             
            // Now add trailing element
            // and check if it is equal
            // to 1 Then increment the
            // value of count_ones by 1
            if(arr[i + x - 1] == 1)
                count_ones++;
             
            if (maxOnes < count_ones)
            maxOnes = count_ones;
        }
         
        // calculate number of zeros in
        // subarray of length x with
        // maximum number of 1's
        int numberOfZeroes = x - maxOnes;
         
        return numberOfZeroes;
    }
     
    // Driver code
    public static void main(String args[])
    {
        int a[] = new int[]{0, 0, 1, 0,
                            1, 1, 0, 0, 1};
        int n = a.length;
         
        System.out.println(minSwaps(a, n));
    }
}
 
// This code is contributed by Sam007

Python3

# Python code for minimum
# swaps required to group
# all 1's together
 
# Function to find minimum
# swaps to group all 1's
# together
def minSwaps(arr, n) :
 
    numberOfOnes = 0
     
    # find total number of
    # all 1's in the array
    for i in range(0, n) :
 
        if (arr[i] == 1) :
            numberOfOnes = numberOfOnes + 1
     
    # length of subarray
    # to check for
    x = numberOfOnes
     
    count_ones = 0
    maxOnes = 0
     
    # Find 1's for first
    # subarray of length x
    for i in range(0, x) :
 
        if(arr[i] == 1) :
            count_ones = count_ones + 1
         
    maxOnes = count_ones
         
    # using sliding window
    # technique to find
    # max number of ones in
    # subarray of length x
    for i in range(1, (n - x + 1)) :
         
        # first remove leading
        # element and check
        # if it is equal to 1
        # then decrement the
        # value of count_ones by 1
        if (arr[i - 1] == 1) :
            count_ones = count_ones - 1
         
        # Now add trailing
        # element and check
        # if it is equal to 1
        # Then increment
        # the value of count_ones by 1
        if(arr[i + x - 1] == 1) :
            count_ones = count_ones + 1
         
        if (maxOnes < count_ones) :
                maxOnes = count_ones
     
    # calculate number of
    # zeros in subarray
    # of length x with
    # maximum number of 1's
    numberOfZeroes = x - maxOnes
     
    return numberOfZeroes
 
# Driver Code
a = [0, 0, 1, 0, 1, 1, 0, 0, 1]
n = 9
print (minSwaps(a, n))
 
# This code is contributed
# by Manish Shaw(manishshaw1)

C#

// C# code for minimum swaps
// required to group all 1's together
using System;
 
class GFG{
     
    // Function to find minimum swaps
    // to group all 1's together
    static int minSwaps(int []arr, int n)
    {
     
        int numberOfOnes = 0;
         
        // find total number of all 1's in the array
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1)
            numberOfOnes++;
        }
         
        // length of subarray to check for
        int x = numberOfOnes;
         
        int count_ones = 0, maxOnes;
         
        // Find 1's for first subarray of length x
        for(int i = 0; i < x; i++){
            if(arr[i] == 1)
            count_ones++;
        }
             
        maxOnes = count_ones;
             
        // using sliding window technique to find
        // max number of ones in subarray of length x
        for (int i = 1; i <= n-x; i++) {
             
            // first remove leading element and check
            // if it is equal to 1 then decrement the
            // value of count_ones by 1
            if (arr[i - 1] == 1)
            count_ones--;
             
            // Now add trailing element and check
            // if it is equal to 1 Then increment
            // the value of count_ones by 1
            if(arr[i + x - 1] == 1)
            count_ones++;
             
            if (maxOnes < count_ones)
            maxOnes = count_ones;
        }
         
        // calculate number of zeros in subarray
        // of length x with maximum number of 1's
        int numberOfZeroes = x - maxOnes;
         
        return numberOfZeroes;
    }
     
    // Driver Code
    static public void Main ()
    {
        int []a = {0, 0, 1, 0, 1, 1, 0, 0, 1};
        int n = a.Length;
     
        Console.WriteLine(minSwaps(a, n));
         
    }
}
// This code is contributed by vt_m.

PHP


Javascript


输出
1

时间复杂度: O(n)
辅助空间: O(1)
感谢Gera 先生提出这种方法。

滑动窗口 易懂版

算法:

1. 将总数存储在变量 say count 中。这将是窗口大小。

2. 初始化一个变量以存储所有大小为 count 的子数组中的最大个数,并初始化一个变量以存储当前窗口中的个数。

3.现在遍历数组,一旦你达到窗口大小,比较该窗口中的数量与迄今为止所有窗口中的最大数量,如果当前窗口中的数量为1,则更新最大数量更多。如果 window 的第一个元素是 1,则减少当前计数。

4. 答案将是个数的总数——所有窗口中的最大个数。

C++

#include 
 
using namespace std;
 
int minSwaps(int arr[], int n)
{
  int totalCount = 0; // To store total number of ones
  // count total no of ones
  for (int i = 0; i < n; i++)
    totalCount += arr[i];
 
  int currCount = 0; // To store count of ones in current window
  int maxCount = 0;  // To store maximum count ones out of all windows
  int i = 0;         // start of window
  int j = 0;         // end of window
 
  while (j < n)
  {
    currCount += arr[j];
 
    // update maxCount when reach window size i.e. total count of ones in array
    if ((j - i + 1) == totalCount)
    {
      maxCount = max(maxCount, currCount);
      if (arr[i] == 1)
        currCount--; // decrease current count if first element of window is 1
      i++;           // slide window
    }
    j++;
  }
 
  return totalCount - maxCount; // return total no of ones in array - maximum count of ones out of all windows
}
 
// Driver Code
int main()
{
 
  int a[] = {1, 0, 1, 0, 1, 1};
  int n = sizeof(a) / sizeof(a[0]);
 
  cout << minSwaps(a, n);
 
  return 0;
}

Java

import java.io.*;
import java.util.*;
 
class GFG{
     
static int minSwaps(int[] arr, int n)
{
     
    // To store total number of ones
    int totalCount = 0;
     
    // Count total no of ones
    int i;
    for(i = 0; i < n; i++)
        totalCount += arr[i];
 
    int currCount = 0; // To store count of ones in current window
    int maxCount = 0;  // To store maximum count ones out
                       // of all windows
                       
    // start of window
    i = 0;
     
    // end of window
    int j = 0;
 
    while (j < n)
    {
        currCount += arr[j];
 
        // update maxCount when reach window size i.e.
        // total count of ones in array
        if ((j - i + 1) == totalCount)
        {
            maxCount = Math.max(maxCount, currCount);
            if (arr[i] == 1)
                currCount--; // decrease current count
                             // if first element of
                             // window is 1
                              
            // slide window
            i++;
        }
        j++;
    }
 
    return totalCount - maxCount; // return total no of ones in array
                                  // - maximum count of ones out of
                                  // all windows
}
 
// Driver Code
public static void main(String args[])
    {
    int[] a = { 1, 0, 1, 0, 1, 1 };
    int n = a.length;
 
    System.out.println(minSwaps(a, n));
}
}
 
// This code is contributed by shivanisinghss2110

Python

def minSwaps(arr, n):
   
    # To store total number of ones
    totalCount = 0
     
    # count total no of ones
    for i in range(0,n):
        totalCount += arr[i]
         
    currCount = 0 # To store count of ones in current window
    maxCount = 0  # To store maximum count ones out of all windows
    i = 0         # start of window
    j = 0         # end of window
     
    while (j < n):
        currCount += arr[j]
         
        # update maxCount when reach window size i.e. total count of ones in array
        if ((j - i + 1) == totalCount):
            maxCount = max(maxCount, currCount)
            if (arr[i] == 1):
                currCount -= 1
                 
                # decrease current count if first element of window is 1
            i += 1           # slide window
        j += 1
     
    return totalCount - maxCount # return total no of ones in array - maximum count of ones out of all windows
 
# Driver Code
a = [1, 0, 1, 0, 1, 1]
n = len(a)
print(minSwaps(a, n))
 
# this code is contributed by shivanisighss2110

C#

using System;
 
class GFG{
     
static int minSwaps(int[] arr, int n)
{
     
    // To store total number of ones
    int totalCount = 0;
     
    // Count total no of ones
    int i;
    for(i = 0; i < n; i++)
        totalCount += arr[i];
 
    int currCount = 0; // To store count of ones in current window
    int maxCount = 0;  // To store maximum count ones out
                       // of all windows
                       
    // start of window
    i = 0;
     
    // end of window
    int j = 0;
 
    while (j < n)
    {
        currCount += arr[j];
 
        // update maxCount when reach window size i.e.
        // total count of ones in array
        if ((j - i + 1) == totalCount)
        {
            maxCount = Math.Max(maxCount, currCount);
            if (arr[i] == 1)
                currCount--; // decrease current count
                             // if first element of
                             // window is 1
                              
            // slide window
            i++;
        }
        j++;
    }
 
    return totalCount - maxCount; // return total no of ones in array
                                  // - maximum count of ones out of
                                  // all windows
}
 
// Driver Code
public static void Main()
{
    int[] a = { 1, 0, 1, 0, 1, 1 };
    int n = a.Length;
 
    Console.WriteLine(minSwaps(a, n));
}
}
 
// This code is contributed by ukasp

Javascript


输出
1

复杂性:

时间:O(n)

空间:O(1)