给定一个二进制数组的大小,该数组仅由0和n组成,该整数由0和n组成;整数m是从0到1允许的翻转次数;任务是将m 0翻转为1后,使任意两个连续的1之间的距离最大。
例子:
Input: n = 5, m = 3
Output: 1
Explanation:
The initial array is arr = { 0, 0, 0, 0, 0 },
The final array is arr = { 1, 0, 1, 0, 1 },
So distance between two consecutive 1’s is 2.
Input: n = 9, m = 3
Output: 4
Explanation:
The initial array is arr = { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
The final array is arr = { 1, 0, 0, 0, 1, 0, 0, 0, 1 },
so distance between two consecutive 1’s 4.
方法:
- 我们可以简单地对任意两个连续的数字之间的距离进行二进制搜索,并检查是否可以将m个零转换为1。
- 首先,我们设置low = 1,high = n – 1
- 然后检查中=(低+高)/ 2是否合适。
- 如果更新后的答案是中,则降低上限=中– 1。
下面是上述方法的实现:
CPP
// C++ program to Maximize distance between
// any two consecutive 1's after flipping M 0's
#include
using namespace std;
// Function to return the count
bool check(int arr[], int n, int m, int d)
{
// Flipping zeros at distance "d"
int i = 0;
while (i < n && m > 0) {
m--;
i += d;
}
return m == 0 ? true : false;
}
// Function to implement
// binary search
int maximumDistance(int arr[], int n, int m)
{
int low = 1, high = n - 1;
int ans = 0;
while (low <= high) {
int mid = (low + high) / 2;
// Check for valid distance i.e mid
bool flag = check(arr, n, m, mid);
if (flag) {
ans = mid;
low = mid + 1;
}
else {
high = mid - 1;
}
}
return ans;
}
// Driver code
int main()
{
int n = 5, m = 3;
int arr[n] = { 0 };
cout << maximumDistance(arr, n, m);
return 0;
}
Java
// Java program to Maximize distance between
// any two consecutive 1's after flipping M 0's
class GFG
{
// Function to return the count
static boolean check(int arr[], int n, int m, int d)
{
// Flipping zeros at distance "d"
int i = 0;
while (i < n && m > 0)
{
m--;
i += d;
}
return m == 0 ? true : false;
}
// Function to implement
// binary search
static int maximumDistance(int arr[], int n, int m)
{
int low = 1, high = n - 1;
int ans = 0;
while (low <= high)
{
int mid = (low + high) / 2;
// Check for valid distance i.e mid
boolean flag = check(arr, n, m, mid);
if (flag)
{
ans = mid;
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int n = 5, m = 3;
int arr[] = new int[n];
System.out.print(maximumDistance(arr, n, m));
}
}
// This code is contributed by 29AjayKumar
Python
# Python3 program to Maximize distance between
# any two consecutive 1's after flipping M 0's
# Function to return the count
def check(arr, n, m, d):
# Flipping zeros at distance "d"
i = 0
while (i < n and m > 0):
m -= 1
i += d
if m == 0:
return True
return False
# Function to implement
# binary search
def maximumDistance(arr, n, m):
low = 1
high = n - 1
ans = 0
while (low <= high):
mid = (low + high) // 2
# Check for valid distance i.e mid
flag = check(arr, n, m, mid)
if (flag) :
ans = mid
low = mid + 1
else :
high = mid - 1
return ans
# Driver code
n = 5
m = 3
arr = [0] * n
print(maximumDistance(arr, n, m))
# This code is contributed by mohit kumar 29
C#
// C# program to Maximize distance between
// any two consecutive 1's after flipping M 0's
using System;
class GFG
{
// Function to return the count
static bool check(int []arr, int n, int m, int d)
{
// Flipping zeros at distance "d"
int i = 0;
while (i < n && m > 0)
{
m--;
i += d;
}
return m == 0 ? true : false;
}
// Function to implement
// binary search
static int maximumDistance(int []arr, int n, int m)
{
int low = 1, high = n - 1;
int ans = 0;
while (low <= high)
{
int mid = (low + high) / 2;
// Check for valid distance i.e mid
bool flag = check(arr, n, m, mid);
if (flag)
{
ans = mid;
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return ans;
}
// Driver code
public static void Main(String[] args)
{
int n = 5, m = 3;
int []arr = new int[n];
Console.Write(maximumDistance(arr, n, m));
}
}
// This code is contributed by PrinciRaj1992
输出:
2
时间复杂度: O(n * log(n))