📌  相关文章
📜  二进制字符串要求的最小翻转,以便所有K大小的子字符串都包含1

📅  最后修改于: 2021-05-25 02:52:23             🧑  作者: Mango

给定大小为N的二进制字符串str和正整数K ,任务是找到使大小为K的所有子字符串至少包含一个“ 1”所需的最小翻转次数。

例子:

方法:
请按照以下步骤解决问题:

  1. 这个想法是使用滑动窗口技术来检查长度K的每个子串是否都包含“ 1”。
  2. 维护变量last_idx以存储字符为“ 1”的窗口的最后一个索引。如果当前窗口中不存在“ 1” ,则此变量的值将为-1
  3. 对于任何这样的窗口,我们将通过将当前窗口的最后一个索引处的字符翻转为“ 1”来增加翻转次数,并将索引last_idx更新为该索引。
  4. 翻转当前窗口的最后一个字符可确保随后的K-1窗口也至少具有一个“ 1”。因此,这种方法使所需的翻转次数最小化。
  5. 对字符串的其余部分重复此过程,并打印所需的最终翻转次数。

下面是上述方法的实现:

C++
// C++ program to find the 
// minimum numbers of flips 
// required in a binary string 
// such that all substrings of 
// size K has atleast one 1 
  
#include  
using namespace std; 
  
// Function to calculate and 
// return the minimum number 
// of flips to make string valid 
int minimumMoves(string S, int K) 
{ 
    int N = S.length(); 
  
    // Stores the count 
    // of required flips 
    int ops = 0; 
  
    // Stores the last index 
    // of '1' in the string 
    int last_idx = -1; 
  
    // Check for the first 
    // substring of length K 
    for (int i = 0; i < K; i++) { 
  
        // If i-th character 
        // is '1' 
        if (S[i] == '1') 
            last_idx = i; 
    } 
  
    // If the substring had 
    // no '1' 
    if (last_idx == -1) { 
  
        // Increase the 
        // count of required 
        // flips 
        ++ops; 
  
        // Flip the last 
        // index of the 
        // window 
        S[K - 1] = '1'; 
  
        // Update the last 
        // index which 
        // contains 1 
        last_idx = K - 1; 
    } 
  
    // Check for remaining substrings 
    for (int i = 1; i < N - K + 1; i++) { 
  
        // If last_idx does not 
        // belong to current 
        // window make it -1 
        if (last_idx < i) 
            last_idx = -1; 
  
        // If the last character of 
        // the current substring 
        // is '1', then update 
        // last_idx to i+k-1; 
        if (S[i + K - 1] == '1') 
            last_idx = i + K - 1; 
  
        // If last_idx == -1, then 
        // the current substring 
        // has no 1 
        if (last_idx == -1) { 
  
            // Increase the count 
            // of flips 
            ++ops; 
  
            // Update the last 
            // index of the 
            // current window 
            S[i + K - 1] = '1'; 
  
            // Store the last 
            // index of current 
            // window as the 
            // index of last '1' 
            // in the string 
            last_idx = i + K - 1; 
        } 
    } 
    // Return the number 
    // of operations 
    return ops; 
} 
  
// Driver Code 
int main() 
{ 
    string S = "001010000"; 
    int K = 3; 
    cout << minimumMoves(S, K); 
    return 0; 
}


Java
// Java program to find the 
// minimum numbers of flips 
// required in a binary string 
// such that all substrings of 
// size K has atleast one 1 
class GFG{ 
      
// Function to calculate and 
// return the minimum number 
// of flips to make string valid 
public static int minimumMoves(String s, int K) 
{ 
    StringBuilder S = new StringBuilder(s); 
    int N = S.length(); 
  
    // Stores the count 
    // of required flips 
    int ops = 0; 
  
    // Stores the last index 
    // of '1' in the string 
    int last_idx = -1; 
  
    // Check for the first 
    // substring of length K 
    for(int i = 0; i < K; i++) 
    { 
          
    // If i-th character 
    // is '1' 
    if (S.charAt(i) == '1') 
        last_idx = i; 
    } 
  
    // If the substring had 
    // no '1' 
    if (last_idx == -1) 
    { 
          
        // Increase the count 
        // of required flips 
        ++ops; 
  
        // Flip the last index 
        // of the window 
        S.setCharAt(K - 1, '1'); 
  
        // Update the last index 
        // which contains 1 
        last_idx = K - 1; 
    } 
  
    // Check for remaining substrings 
    for(int i = 1; i < N - K + 1; i++) 
    { 
          
    // If last_idx does not 
    // belong to current 
    // window make it -1 
    if (last_idx < i) 
        last_idx = -1; 
          
    // If the last character of 
    // the current substring 
    // is '1', then update 
    // last_idx to i+k-1; 
    if (S.charAt(i + K - 1) == '1') 
        last_idx = i + K - 1; 
          
    // If last_idx == -1, then 
    // the current substring 
    // has no 1 
    if (last_idx == -1) 
    { 
              
        // Increase the count 
        // of flips 
        ++ops; 
              
        // Update the last index 
        // of the current window 
        S.setCharAt(i + K - 1, '1'); 
              
        // Store the last index 
        // of current window as 
        // the index of last '1' 
        // in the string 
        last_idx = i + K - 1; 
    } 
    } 
      
    // Return the number 
    // of operations 
    return ops; 
} 
  
// Driver Code 
public static void main(String[] args) 
{ 
    String S = "001010000"; 
    int K = 3; 
      
    System.out.println(minimumMoves(S, K)); 
} 
} 
  
// This code is contributed by jrishabh99


Python3
# Python3 program to find the minimum 
# numbers of flips required in a binary 
# string such that all substrings of 
# size K has atleast one 1 
  
# Function to calculate and 
# return the minimum number 
# of flips to make string valid 
def minimumMoves(S, K): 
      
    N = len(S) 
  
    # Stores the count 
    # of required flips 
    ops = 0
      
    # Stores the last index 
    # of '1' in the string 
    last_idx = -1
  
    # Check for the first 
    # substring of length K 
    for i in range(K): 
  
        # If i-th character 
        # is '1' 
        if (S[i] == '1'): 
            last_idx = i 
      
    # If the substring had 
    # no '1' 
    if (last_idx == -1): 
  
        # Increase the count 
        # of required flips 
        ops += 1
  
        # Flip the last index 
        # of the window 
        S[K - 1] = '1'
  
        # Update the last index 
        # which contains 1 
        last_idx = K - 1
  
    # Check for remaining substrings 
    for i in range(N - K + 1): 
          
        # If last_idx does not 
        # belong to current 
        # window make it -1 
        if (last_idx < i): 
            last_idx = -1
  
        # If the last character of 
        # the current substring 
        # is '1', then update 
        # last_idx to i + k-1; 
        if (S[i + K - 1] == '1'): 
            last_idx = i + K - 1
  
        # If last_idx == -1, then 
        # the current substring 
        # has no 1 
        if (last_idx == -1): 
  
            # Increase the count 
            # of flips 
            ops += 1
  
            # Update the last index 
            # of the current window 
            S = S[:i + K - 1] + '1' + S[i + K:] 
          
            # Store the last index of 
            # current window as the index 
            # of last '1' in the string 
            last_idx = i + K - 1
              
    # Return the number 
    # of operations 
    return ops 
  
# Driver Code 
S = "001010000"
K = 3; 
  
print(minimumMoves(S, K)) 
  
# This code is contributed by yatinagg


C#
// C# program to find the
// minimum numbers of flips
// required in a binary string
// such that all substrings of
// size K has atleast one 1
using System;
using System.Text;
  
class GFG{
      
// Function to calculate and
// return the minimum number
// of flips to make string valid
public static int minimumMoves(String s, int K)
{
    StringBuilder S = new StringBuilder(s);
    int N = S.Length;
  
    // Stores the count
    // of required flips
    int ops = 0;
  
    // Stores the last index
    // of '1' in the string
    int last_idx = -1;
  
    // Check for the first
    // substring of length K
    for(int i = 0; i < K; i++)
    {
          
        // If i-th character
        // is '1'
        if (S[i] == '1')
            last_idx = i;
    }
  
    // If the substring had
    // no '1'
    if (last_idx == -1)
    {
          
        // Increase the count
        // of required flips
        ++ops;
  
        // Flip the last index 
        // of the window
        S.Insert(K - 1, '1');
  
        // Update the last index 
        // which contains 1
        last_idx = K - 1;
    }
  
    // Check for remaining substrings
    for(int i = 1; i < N - K + 1; i++)
    {
          
        // If last_idx does not
        // belong to current
        // window make it -1
        if (last_idx < i)
            last_idx = -1;
              
        // If the last character of
        // the current substring
        // is '1', then update
        // last_idx to i+k-1;
        if (S[i + K - 1] == '1')
            last_idx = i + K - 1;
              
        // If last_idx == -1, then
        // the current substring
        // has no 1
        if (last_idx == -1)
        {
                  
            // Increase the count
            // of flips
            ++ops;
                  
            // Update the last index
            // of the current window
            S.Insert(i + K - 1, '1');
                  
            // Store the last index 
            // of current window as
            // the index of last '1'
            // in the string
            last_idx = i + K - 1;
        }
    }
      
    // Return the number
    // of operations
    return ops;
}
  
// Driver Code
public static void Main(String[] args)
{
    String S = "001010000";
    int K = 3;
      
    Console.WriteLine(minimumMoves(S, K));
}
}
  
// This code is contributed by gauravrajput1


输出:
1

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。