📌  相关文章
📜  在二进制循环数组中将所有 0 组合在一起的最小交换

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

在二进制循环数组中将所有 0 组合在一起的最小交换

给定一个大小为N二进制循环数组arr[] ,任务是找到最小交换以将数组中的所有0组合在一起。

例子

方法:该任务可以使用滑动窗口技术来解决。请按照以下步骤解决问题:

  • 计算 0 的总数。让 m 是那个数字
  • 找到其中包含最多0的长度为m连续区域
  • 该区域中1的数量是所需的最少交换。每次交换都会将一个 0 移入该区域,并将一个 1 移出该区域。
  • 最后,使用模运算来处理循环数组的情况。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count min swap
// to get all zeros together
int minSwaps(int nums[], int N)
{
    int count_1 = 0;
 
    if (N == 1)
        return 0;
 
    for (int i = 0; i < N; i++) {
        if (nums[i] == 0)
            count_1++;
    }
 
    // Window size for counting
    // maximum  no. of 1s
    int windowsize = count_1;
    count_1 = 0;
 
    for (int i = 0; i < windowsize; i++) {
        if (nums[i] == 0)
            count_1++;
    }
 
    // For storing maximum count of 1s in
    // a window
    int mx = count_1;
    for (int i = windowsize; i < N + windowsize; i++) {
        if (nums[i % N] == 0)
            count_1++;
        if (nums[(i - windowsize) % N] == 0)
            count_1--;
        mx = max(count_1, mx);
    }
    return windowsize - mx;
}
 
// Driver code
int main()
{
    int nums[] = { 1, 0, 1, 0, 0, 1, 1 };
    int N = sizeof(nums) / sizeof(nums[0]);
    cout << minSwaps(nums, N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to count min swap
  // to get all zeros together
  static int minSwaps(int nums[], int N)
  {
    int count_1 = 0;
 
    if (N == 1)
      return 0;
 
    for (int i = 0; i < N; i++) {
      if (nums[i] == 0)
        count_1++;
    }
 
    // Window size for counting
    // maximum  no. of 1s
    int windowsize = count_1;
    count_1 = 0;
 
    for (int i = 0; i < windowsize; i++) {
      if (nums[i] == 0)
        count_1++;
    }
 
    // For storing maximum count of 1s in
    // a window
    int mx = count_1;
    for (int i = windowsize; i < N + windowsize; i++) {
      if (nums[i % N] == 0)
        count_1++;
      if (nums[(i - windowsize) % N] == 0)
        count_1--;
      mx = Math.max(count_1, mx);
    }
    return windowsize - mx;
  }
 
  // Driver code
  public static void main (String[] args) {
    int nums[] = { 1, 0, 1, 0, 0, 1, 1 };
    int N = nums.length;
    System.out.println( minSwaps(nums, N));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# python3 program for the above approach
 
# Function to count min swap
# to get all zeros together
def minSwaps(nums, N):
 
    count_1 = 0
 
    if (N == 1):
        return 0
 
    for i in range(0, N):
        if (nums[i] == 0):
            count_1 += 1
 
    # Window size for counting
    # maximum no. of 1s
    windowsize = count_1
    count_1 = 0
 
    for i in range(0, windowsize):
        if (nums[i] == 0):
            count_1 += 1
 
    # For storing maximum count of 1s in
    # a window
    mx = count_1
    for i in range(windowsize, N + windowsize):
        if (nums[i % N] == 0):
            count_1 += 1
        if (nums[(i - windowsize) % N] == 0):
            count_1 -= 1
        mx = max(count_1, mx)
 
    return windowsize - mx
 
# Driver code
if __name__ == "__main__":
 
    nums = [1, 0, 1, 0, 0, 1, 1]
    N = len(nums)
    print(minSwaps(nums, N))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to count min swap
  // to get all zeros together
  static int minSwaps(int []nums, int N)
  {
    int count_1 = 0;
 
    if (N == 1)
      return 0;
 
    for (int i = 0; i < N; i++) {
      if (nums[i] == 0)
        count_1++;
    }
 
    // Window size for counting
    // maximum  no. of 1s
    int windowsize = count_1;
    count_1 = 0;
 
    for (int i = 0; i < windowsize; i++) {
      if (nums[i] == 0)
        count_1++;
    }
 
    // For storing maximum count of 1s in
    // a window
    int mx = count_1;
    for (int i = windowsize; i < N + windowsize; i++) {
      if (nums[i % N] == 0)
        count_1++;
      if (nums[(i - windowsize) % N] == 0)
        count_1--;
      mx = Math.Max(count_1, mx);
    }
    return windowsize - mx;
  }
 
  // Driver code
  public static void Main()
  {
    int []nums = { 1, 0, 1, 0, 0, 1, 1 };
    int N = nums.Length;
    Console.Write(minSwaps(nums, N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
1

时间复杂度 O(N)
辅助空间 O(1)