📌  相关文章
📜  使给定二进制数组交替所需的最小子数组反转

📅  最后修改于: 2021-05-13 23:52:46             🧑  作者: Mango

给定一个二进制数组arr [],该数组包含等于0 s和1 s的相等计数,任务是计算使二进制数组交替所需的最小子数组反转操作数。在每个操作中,反转给定数组的任何子数组。

例子:

方法:可以使用贪婪技术解决问题。想法是对不在以正确的索引表示的数组元素进行计数,以便对要交替的数组进行计数,即对给定数组中存在的相等的连续元素进行计数。请按照以下步骤解决问题:

  • 初始化一个变量,例如cntOp ,以存储使给定数组交替所需的最小子数组反转操作数。
  • 使用变量i遍历数组,对于每个数组元素arr [i] ,检查其是否等于arr [i + 1] 。如果发现为真,则增加cntOp的值。
  • 最后,打印(cntOp +1)/ 2的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to count minimum subarray reversal
// operations required to make array alternating
int minimumcntOperationReq(int arr[], int N)
{
    // Stores minimum count of operations
    // required to make array alternating
    int cntOp = 0;
 
    // Traverse the array
    for (int i = 0; i < N - 1; i++) {
 
        // If arr[i] is greater
        // than arr[i + 1]
        if (arr[i] == arr[i + 1]) {
 
            // Update cntOp
            cntOp++;
        }
    }
 
    return (cntOp + 1) / 2;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 1, 1, 0, 1, 0, 0, 0 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << minimumcntOperationReq(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
  
class GFG{
  
// Function to count minimum subarray reversal
// operations required to make array alternating
static int minimumcntOperationReq(int arr[], int N)
{
     
    // Stores minimum count of operations
    // required to make array alternating
    int cntOp = 0;
  
    // Traverse the array
    for(int i = 0; i < N - 1; i++)
    {
         
        // If arr[i] is greater
        // than arr[i + 1]
        if (arr[i] == arr[i + 1])
        {
             
            // Update cntOp
            cntOp++;
        }
    }
    return (cntOp + 1) / 2;
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 1, 1, 0, 1, 0, 0, 0 };
    int N = arr.length;
  
    System.out.print(minimumcntOperationReq(arr, N));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement
# the above approach
  
# Function to count minimum subarray
# reversal operations required to make
# array alternating
def minimumcntOperationReq(arr, N):
     
    # Stores minimum count of operations
    # required to make array alternating
    cntOp = 0
  
    # Traverse the array
    for i in range(N - 1):
  
        # If arr[i] is greater
        # than arr[i + 1]
        if (arr[i] == arr[i + 1]):
  
            # Update cntOp
            cntOp += 1
             
    return (cntOp + 1) // 2
 
# Driver Code
arr = [ 1, 1, 1, 0, 1, 0, 0, 0 ]
  
N = len(arr)
  
print(minimumcntOperationReq(arr, N))
 
# This code is contributed by susmitakundugoaldanga


C#
// C# program to implement
// the above approach
using System;
class GFG
{
     
    // Function to count minimum subarray reversal
    // operations required to make array alternating
    static int minimumcntOperationReq(int[] arr, int N)
    {
       
        // Stores minimum count of operations
        // required to make array alternating
        int cntOp = 0;
      
        // Traverse the array
        for (int i = 0; i < N - 1; i++)
        {
      
            // If arr[i] is greater
            // than arr[i + 1]
            if (arr[i] == arr[i + 1])
            {
      
                // Update cntOp
                cntOp++;
            }
        }
      
        return (cntOp + 1) / 2;
    }
   
  // Driver code
  static void Main()
  {
        int[] arr = { 1, 1, 1, 0, 1, 0, 0, 0 };
  
        int N = arr.Length;
      
        Console.WriteLine(minimumcntOperationReq(arr, N));
  }
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
2

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