给定一个由N 个整数组成的二进制数组arr[] ,任务是计算将数组的所有元素转换为等于K所需的以下类型的最小操作数:
- 从给定数组中选择任何索引X。
- 翻转子数组 arr[X] … arr[N – 1] 的所有元素,即如果 arr[i] = 1,则将 arr[i] 设置为 0,反之亦然。
例子:
Input: N = 8, arr[ ] = {1, 0, 1, 0, 0, 1, 1, 1}, K = 0
Output:5
Explanation:
Operation 1: X = 0 (chosen index). After modifying values the updated array arr[] is [0, 1, 0, 1, 1, 0, 0, 0].
Operation 2: X = 1 (chosen index). After modifying values the updated array arr[] is [0, 0, 1, 0, 0, 1, 1, 1].
Operation 3: X = 2 (chosen index). After modifying values the updated array arr[] is [0, 0, 0, 1, 1, 0, 0, 0].
Operation 4: X = 3 (chosen index). After modifying values the updated array arr[] is [0, 0, 0, 0, 0, 1, 1, 1].
Operation 5: X = 5 (chosen index). After modifying values the updated array arr[] is [0, 0, 0, 0, 0, 0, 0, 0].
Input: N = 8, arr[ ] = {1, 0, 1, 0, 0, 1, 1, 1}, K = 1
Output:4
方法:应进行以下观察:
As any index X ( < N) can be chosen and each value from index X to the index N-1 can be modified, so it can be found that the approach is to count the number of changing points in the array.
请按照以下步骤解决上述问题:
- 将变量标志初始化为K 的倒数。即如果 K = 0 则为 1,反之亦然,表示当前值。
- 将变量cnt初始化为 0,以保持数组 arr[] 中变化点数的计数。
- 遍历数组ARR []以及对于每个索引i应用以下步骤:
- 如果flag值和arr[i]值不同,则进入下一次迭代。
- 如果flag和arr[i]相等,则增加count并将flag设置为flag = (flag + 1) % 2 。
- 打印count的最终值。
下面是上述方法的实现:
C++14
// C++14 Program to implement
// the above approach
#include
using namespace std;
// Function to count the minimum
// number of subarray flips required
int minSteps(int arr[], int n, int k)
{
int i, cnt = 0;
int flag;
if (k == 1)
flag = 0;
else
flag = 1;
// Iterate the array
for (i = 0; i < n; i++) {
// If arr[i] and flag are equal
if (arr[i] == flag) {
cnt++;
flag = (flag + 1) % 2;
}
}
// Return the answer
return cnt;
}
// Driver Code
int main()
{
int arr[] = { 1, 0, 1, 0, 0, 1, 1, 1 };
int n = sizeof(arr)
/ sizeof(arr[0]);
int k = 1;
cout << minSteps(arr, n, k);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to count the minimum
// number of subarray flips required
static int minSteps(int arr[], int n, int k)
{
int i, cnt = 0;
int flag;
if (k == 1)
flag = 0;
else
flag = 1;
// Iterate the array
for(i = 0; i < n; i++)
{
// If arr[i] and flag are equal
if (arr[i] == flag)
{
cnt++;
flag = (flag + 1) % 2;
}
}
// Return the answer
return cnt;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 1, 0, 1, 0, 0, 1, 1, 1 };
int n = arr.length;
int k = 1;
System.out.print(minSteps(arr, n, k));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to implement
# the above approach
# Function to count the minimum
# number of subarray flips required
def minSteps(arr, n, k):
cnt = 0
if(k == 1):
flag = 0
else:
flag = 1
# Iterate the array
for i in range(n):
# If arr[i] and flag are equal
if(arr[i] == flag):
cnt += 1
flag = (flag + 1) % 2
# Return the answer
return cnt
# Driver Code
arr = [ 1, 0, 1, 0, 0, 1, 1, 1 ]
n = len(arr)
k = 1
# Function call
print(minSteps(arr, n, k))
# This code is contributed by Shivam Singh
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to count the minimum
// number of subarray flips required
static int minSteps(int[] arr, int n, int k)
{
int i, cnt = 0;
int flag;
if (k == 1)
flag = 0;
else
flag = 1;
// Iterate the array
for(i = 0; i < n; i++)
{
// If arr[i] and flag are equal
if (arr[i] == flag)
{
cnt++;
flag = (flag + 1) % 2;
}
}
// Return the answer
return cnt;
}
// Driver code
public static void Main ()
{
int[] arr = { 1, 0, 1, 0, 0, 1, 1, 1 };
int n = arr.Length;
int k = 1;
Console.Write(minSteps(arr, n, k));
}
}
// This code is contributed by chitranayal
Javascript
4
时间复杂度: O(N)
辅助空间: O(1)