给定一个由0 s 和1 s 组成的二进制数组arr[] ,任务是计算使二进制数组交替所需的最小子数组反转操作次数。在每个操作中反转给定数组的任何子数组。
例子:
Input: arr[] = { 1, 1, 1, 0, 1, 0, 0, 0 }
Output: 2
Explanation:
Reversing the subarray {arr[1], …, arr[5]} modifies arr[] to { 1, 0, 1, 0, 1, 1, 0, 0 }
Reversing the subarray {arr[5], arr[6]} modifies arr[] to { 1, 0, 1, 0, 1, 0, 1, 0 }, which is alternating. Therefore, the required output is 2.
Input: arr[] = { 0, 1, 1, 0 }
Output: 1
Explanation:
Reversing the subarray {arr[2], …, arr[2]} modifies arr[] to { 0, 1, 0, 1 }, which is alternating. Therefore, the required output is 1.
方法:该问题可以使用贪心技术解决。这个想法是计算不存在于正确索引处的数组元素以供交替使用,即计算给定数组中出现的连续相等元素。请按照以下步骤解决问题:
- 初始化一个变量,比如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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live