检查是否可以翻转 K 0,使得给定的 Array 没有相邻的 1
给定一个大小为N的二进制数组arr[]和一个整数K ,任务是检查K 0 是否可以翻转,使得该数组没有相邻的 1。
例子:
Input: arr[] = {0, 0, 0, 0, 1}, K=2
Output: true
Explanation: The 0 at indices 0 and 2 can be replaced by 1.
Hence 2 elements can be flipped (=K).
Input: arr[] = {1, 0, 0, 0, 1}, K = 2
Output: false
Explanation: The 0 at index 2 can be replaced by 1.
Hence 1 element can be flipped (!= K).
方法:解决方案基于贪婪方法。请按照以下步骤操作:
- 遍历数组,对于每个索引为 0 的索引,检查其相邻的两个索引是否为 0。对于每个可能的翻转,减少 K 的计数。
- 对于数组的最后一个和第一个索引,分别检查相邻的左右索引。
- 对于满足上述条件的每个此类索引,如果可能,减少 K 的计数。
- 如果 K<=0 返回真,否则返回假。
下面是上述方法的实现。
C++
// C++ code to implement the approach
#include
using namespace std;
// Function to check
// the possibility of K flips
bool flips(vector& arr, int K)
{
if (arr[0] == 0 && (arr.size() == 1
|| arr[1] == 0)) {
arr[0] = 1;
K--;
}
for (int i = 1; i < arr.size() - 1;
i++) {
if (arr[i - 1] == 0 && arr[i] == 0
&& arr[i + 1] == 0) {
arr[i] = 1;
K--;
}
}
if (arr.size() > 1
&& arr[arr.size() - 2] == 0
&& arr[arr.size() - 1] == 0) {
arr[arr.size() - 1] = 1;
K--;
}
return K <= 0;
}
// Driver code
int main()
{
vector arr = { 0, 0, 0, 0, 1 };
int K = 2;
cout << (flips(arr, K) ? "true" : "false");
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to check
// the possibility of K flips
static Boolean flips(int arr[], int K)
{
if (arr[0] == 0 && (arr.length == 1
|| arr[1] == 0)) {
arr[0] = 1;
K--;
}
for (int i = 1; i < arr.length - 1;
i++) {
if (arr[i - 1] == 0 && arr[i] == 0
&& arr[i + 1] == 0) {
arr[i] = 1;
K--;
}
}
if (arr.length > 1
&& arr[arr.length - 2] == 0
&& arr[arr.length - 1] == 0) {
arr[arr.length - 1] = 1;
K--;
}
return K <= 0;
}
// Driver code
public static void main (String[] args) {
int arr[] = { 0, 0, 0, 0, 1 };
int K = 2;
System.out.println(flips(arr, K) ? "true" : "false");
}
}
// This code is contributed by hrithikgarg03188
Python3
# Python code for the above approach
# Function to check
# the possibility of K flips
def flips(arr, K):
if (arr[0] == 0 and (len(arr) == 1 or arr[1] == 0)):
arr[0] = 1;
K -= 1
for i in range(1, len(arr) - 1):
if (arr[i - 1] == 0 and arr[i] == 0
and arr[i + 1] == 0):
arr[i] = 1;
K -= 1
if (len(arr) > 1
and arr[len(arr) - 2] == 0
and arr[len(arr) - 1] == 0):
arr[len(arr) - 1] = 1;
K -= 1
return K <= 0;
# Driver code
arr = [0, 0, 0, 0, 1];
K = 2;
if (flips(arr, K)):
print("true");
else:
print("false");
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG {
// Function to check
// the possibility of K flips
static Boolean flips(int[] arr, int K)
{
if (arr[0] == 0 && (arr.Length == 1 || arr[1] == 0)) {
arr[0] = 1;
K--;
}
for (int i = 1; i < arr.Length - 1;
i++) {
if (arr[i - 1] == 0 && arr[i] == 0
&& arr[i + 1] == 0) {
arr[i] = 1;
K--;
}
}
if (arr.Length > 1
&& arr[arr.Length - 2] == 0
&& arr[arr.Length - 1] == 0) {
arr[arr.Length - 1] = 1;
K--;
}
return K <= 0;
}
// Driver code
public static void Main () {
int[] arr = { 0, 0, 0, 0, 1 };
int K = 2;
Console.Write(flips(arr, K) ? "true" : "false");
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出
true
时间复杂度: O(N)
辅助空间: O(1)