📌  相关文章
📜  生成 0 和 1 的连续子串所需的最小翻转次数

📅  最后修改于: 2021-10-26 05:35:05             🧑  作者: Mango

给定长度为N的二进制字符串S ,任务是找到转换给定字符串所需的最小位翻转次数,使其仅包含 0 和 1 的连续子字符串,从而最终字符串的形式为000.. 000111..111111…000000…111

例子:

方法:
可以在两次线性遍历中有效地计算最小翻转次数。
在第一次遍历中,我们将计算最坏情况下所需的最小翻转次数,因为它可以等于初始 0 的总数
在第二次遍历中,在每一步,所需的翻转总数将是该点之前的总 1 和该点之后的总 0 之和。我们将在每一步计算的所有值中取最小值。

因此,要解决此问题,请按照以下步骤操作:

  • 初始化变量count0 = 0、 count1 = 0 和res = 0。其中,count0 存储 0 的计数,count1 存储 1 的计数,res 存储所需的位翻转。
  • 遍历输入字符串,计算 0 并将其存储在 res 变量中。
  • 如果找到字符0,则遍历输入字符串并减去 0 的计数,并将字符1 的计数存储在变量 count1 中,并将 res 更新为min(res, count0+count1)

下面是上述方法的实现。

C++
// C++ implementation of the above approach
 
#include 
using namespace std;
 
int minChanges(string str, int N)
{
    int res;
    int count0 = 0, count1 = 0;
 
    // Traverse input string
    // and store the count of 0
    for (char x : str) {
        count0 += (x == '0');
    }
    res = count0;
 
    // Traverse the input string again
    // to find minimum number of flips
    for (char x : str) {
        count0 -= (x == '0');
        count1 += (x == '1');
        res = min(res, count1 + count0);
    }
 
    return res;
}
 
// Driver code
int main()
{
    int N = 9;
    string str = "000101001";
 
    cout << minChanges(str, N);
    return 0;
}


Java
// Java implementation of the above approach
import java.io.*;
 
class GFG{
 
static int minChanges(String str, int N)
{
    int res;
    int count0 = 0, count1 = 0;
 
    // Traverse input string
    // and store the count of 0
    for(char x : str.toCharArray())
    {
       if (x == '0')
           count0++;
    }
    res = count0;
 
    // Traverse the input string again
    // to find minimum number of flips
    for(char x : str.toCharArray())
    {
       if (x == '0')
           count0--;
       if (x == '1')
           count1++;
            
       res = Math.min(res, count1 + count0);
    }
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 9;
    String str = "000101001";
 
    System.out.println(minChanges(str, N));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 implementation of the above approach
def minChanges(str, N):
     
    count0 = 0
    count1 = 0
     
    # Traverse input string
    # and store the count of 0
    for x in str:
        count0 += (x == '0')
 
    res = count0
             
    # Traverse the input string again
    # to find minimum number of flips
    for x in str:
        count0 -= (x == '0')
        count1 += (x == '1')
        res = min(res, count1 + count0)
         
    return res
 
 
# Driver code
N = 9
str = "000101001"
 
print(minChanges(str, N))
 
# This code is contributed by shubhamsingh10


C#
// C# implementation of the above approach
using System;
 
class GFG{
 
static int minChanges(String str, int N)
{
    int res;
    int count0 = 0, count1 = 0;
 
    // Traverse input string
    // and store the count of 0
    for(int i = 0; i < str.Length; i++)
    {
        if (str[i] == '0')
            count0++;
    }
    res = count0;
 
    // Traverse the input string again
    // to find minimum number of flips
    for(int i = 0; i< str.Length; i++)
    {
        if (str[i] == '0')
            count0--;
        if (str[i] == '1')
            count1++;
                 
        res = Math.Min(res, count1 + count0);
    }
    return res;
}
 
// Driver code
public static void Main()
{
    int N = 9;
    String str = "000101001";
 
    Console.Write(minChanges(str, N));
}
}
 
// This code is contributed by chitranayal


Javascript


输出:
2

时间复杂度: O(k) ,其中,k 是二进制字符串的长度。
空间复杂度: O(1)