给定的二进制字符串s和数量K,任务是找到可以通过使得两个相邻的1秒由至少k 0在它们之间的分离1S替换的0的最大数目。
例子:
Input: K = 2, s = “000000”
Output: 2
Explanation:
Change the 0s at position 0 and 3. Then the final string will be “100100” such that every 1 is separated by at least 2 0s.
Input: K = 1, s = “01001000100000”
Output: 3
Explanation:
Change the 0s at position 6, 10, and 12. Then the final string will be “01001010101010” such that every 1 is separated by at least 1 0s.
方法:
- 将给定字符串的所有字符插入一个数组(比如arr[]) 。
- 遍历数组arr[]并将位于<= K位置附近的所有0转换为-1已经存在的1 。此操作给出了可以插入1的字符串的所有可能位置。
- 将计数变量初始化为 0。
- 从左到尾遍历数组arr[] 。一旦遇到第一个 0,就将其替换为 1 并增加计数值。
- 将位于新转换的 1 附近<= K 个位置的所有0转换为-1 。
- 继续遍历数组直到结束,每次遇到 0 时,重复步骤4 – 5 。
下面是上述方法的实现:
C++
// C++ program for the above problem
#include
using namespace std;
// Function to find the
// count of 0s to be flipped
int count(int k, string s)
{
int ar[s.length()];
int end = 0;
// Loop traversal to mark K
// adjacent positions to the right
// of already existing 1s.
for(int i = 0; i < s.length(); i++)
{
if (s[i] == '1')
{
for(int j = i;
j < s.length() &&
j <= i + k; j++)
{
ar[j] = -1;
end = j;
}
i = end;
}
}
end = 0;
// Loop traversal to mark K
// adjacent positions to the left
// of already existing 1s.
for(int i = s.length() - 1;
i >= 0; i--)
{
if (s[i] == '1')
{
for(int j = i;
j >= 0 &&
j >= i - k; j--)
{
ar[j] = -1;
end = j;
}
i = end;
}
}
int ans = 0;
end = 0;
// Loop to count the maximum
// number of 0s that will be
// replaced by 1s
for(int j = 0;
j < s.length(); j++)
{
if (ar[j] == 0)
{
ans++;
for(int g = j;
g <= j + k &&
g < s.length(); g++)
{
ar[g] = -1;
end = g;
}
j = end - 1;
}
}
return ans;
}
// Driver code
int main()
{
int K = 2;
string s = "000000";
cout << count(K, s) << endl;
return 0;
}
// This code is contributed by divyeshrabadiya07
Java
// Java program for the above problem
import java.util.Scanner;
// Driver Code
public class Check {
// Function to find the
// count of 0s to be flipped
public static int count(int k, String s)
{
int ar[] = new int[s.length()];
int end = 0;
// Loop traversal to mark K
// adjacent positions to the right
// of already existing 1s.
for (int i = 0;
i < s.length(); i++) {
if (s.charAt(i) == '1') {
for (int j = i;
j < s.length()
&& j <= i + k;
j++) {
ar[j] = -1;
end = j;
}
i = end;
}
}
end = 0;
// Loop traversal to mark K
// adjacent positions to the left
// of already existing 1s.
for (int i = s.length() - 1;
i >= 0; i--) {
if (s.charAt(i) == '1') {
for (int j = i;
j >= 0 && j >= i - k;
j--) {
ar[j] = -1;
end = j;
}
i = end;
}
}
int ans = 0;
end = 0;
// Loop to count the maximum
// number of 0s that will be
// replaced by 1s
for (int j = 0;
j < s.length(); j++) {
if (ar[j] == 0) {
ans++;
for (int g = j;
g <= j + k
&& g < s.length();
g++) {
ar[g] = -1;
end = g;
}
j = end - 1;
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int K = 2;
String s = "000000";
System.out.println(count(K, s));
}
}
Python3
# Python3 program for the above problem
# Function to find the
# count of 0s to be flipped
def count(k, s):
ar = [0] * len(s)
end = 0
# Loop traversal to mark K
# adjacent positions to the right
# of already existing 1s.
for i in range(len(s)):
if s[i] == '1':
for j in range(i, len(s)):
if (j <= i + k):
ar[j] = -1
end = j
i = end
end = 0
# Loop traversal to mark K
# adjacent positions to the left
# of already existing 1s.
for i in range(len(s) - 1, -1, -1):
if (s[i] == '1'):
for j in range(i, -1, -1):
if (j >= i - k):
ar[j] = -1
end = j
i = end
ans = 0
end = 0
# Loop to count the maximum
# number of 0s that will be
# replaced by 1s
for j in range(len(s)):
if (ar[j] == 0):
ans += 1
for g in range(j, len(s)):
if (g <= j + k):
ar[g] = -1
end = g
j = end - 1
return ans
# Driver code
K = 2
s = "000000"
print(count(K, s))
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above problem
using System;
class GFG{
// Function to find the
// count of 0s to be flipped
public static int count(int k, String s)
{
int []ar = new int[s.Length];
int end = 0;
// Loop traversal to mark K
// adjacent positions to the right
// of already existing 1s.
for(int i = 0; i < s.Length; i++)
{
if (s[i] == '1')
{
for(int j = i;
j < s.Length &&
j <= i + k; j++)
{
ar[j] = -1;
end = j;
}
i = end;
}
}
end = 0;
// Loop traversal to mark K
// adjacent positions to the left
// of already existing 1s.
for(int i = s.Length - 1; i >= 0; i--)
{
if (s[i] == '1')
{
for(int j = i;
j >= 0 &&
j >= i - k; j--)
{
ar[j] = -1;
end = j;
}
i = end;
}
}
int ans = 0;
end = 0;
// Loop to count the maximum
// number of 0s that will be
// replaced by 1s
for(int j = 0; j < s.Length; j++)
{
if (ar[j] == 0)
{
ans++;
for(int g = j;
g <= j + k &&
g < s.Length; g++)
{
ar[g] = -1;
end = g;
}
j = end - 1;
}
}
return ans;
}
// Driver code
public static void Main(String[] args)
{
int K = 2;
String s = "000000";
Console.WriteLine(count(K, s));
}
}
// This code is contributed by amal kumar choubey
Javascript
输出:
2
时间复杂度: O(N * K)
辅助空间: O(1)