📌  相关文章
📜  翻转0的计数以使任意两个相邻的1至少相隔K 0s

📅  最后修改于: 2021-05-14 08:55:37             🧑  作者: Mango

给定二进制字符串s和数字K,任务是找到可以用1代替的最大0 ,以使两个相邻的1之间至少被K 0隔开。
例子:

方法:

  1. 将给定字符串的所有字符插入数组(例如arr [])
  2. 遍历数组arr []并将所有0转换为-1 ,它们位于已经存在的1附近的<= K个位置。此操作给出了可以插入1的字符串的所有可能位置。
  3. 计数变量初始化为0。
  4. 从左到尾遍历数组arr [] 。遇到第一个0时,立即将其替换为1并增加计数值。
  5. 将位于新转换的1附近<= K个位置的所有0转换为-1
  6. 继续遍历数组直到结尾,每次遇到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


输出:
2


时间复杂度: O(N * K)
辅助空间: O(1)