📌  相关文章
📜  将所有 1 保持在二进制字符串所需的最小翻转次数

📅  最后修改于: 2021-09-17 07:43:44             🧑  作者: Mango

给定二进制字符串str ,任务是找到在给定二进制字符串中将所有 1 保持在一起所需的最小翻转次数,即字符串的1 之间不能有任何 0。
例子:

方法:为了解决上面提到的问题,我们将实现动态规划方法,其中我们将有以下状态:

  • 第一个状态是 dp[i][0] ,它表示将所有零变为第 i 位所需的翻转次数。
  • 第二个状态 dp[i][1]表示使当前位为 1 所需的翻转次数,从而满足问题中给出的条件。

因此,所需的答案将是使当前位为 1 的最小翻转次数 + 使当前位 0 之后的所有位为 i 的所有值的最小翻转次数。但是如果给定字符串中的所有位都是 0,那么我们就不必更改任何内容,因此我们可以检查我们的答案与使字符串全部为零所需的翻转次数之间的最小值。所以我们可以通过迭代字符串中的所有字符来计算答案,其中,

下面是上述方法的实现:

C++
//cpp implementation for Minimum number of
//flips required in a binary string such
//that all the 1’s are together
#include 
using namespace std;
int minFlip(string a)
{
     //Length of the binary string
    int n = a.size();
 
    vector> dp(n + 1,vector(2, 0));
 
    //Initial state of the dp
    //dp[0][0] will be 1 if the current
    //bit is 1 and we have to flip it
    dp[0][0] = (a[0] == '1');
 
    //Initial state of the dp
    //dp[0][1] will be 1 if the current
    //bit is 0 and we have to flip it
    dp[0][1] = (a[0] == '0');
 
 
    for (int i = 1; i < n; i++)
    {
        //dp[i][0] = Flips required to
        //make all previous bits zero
        //+ Flip required to make current bit zero
        dp[i][0] = dp[i - 1][0] + (a[i] == '1');
 
 
        //dp[i][1] = mimimum flips required
        //to make all previous states 0 or make
        //previous states 1 satisfying the condition
        dp[i][1] = min(dp[i - 1][0],
                       dp[i - 1][1]) + (a[i] == '0');
 
    }
    int answer = INT_MAX;
    for (int i=0;i


Java
// Java implementation for
// Minimum number of flips
// required in a binary string
// such that all the 1’s are together
import java.io.*;
import java.util.*;
class GFG{
     
static int minFlip(String a)
{
  // Length of the binary string
  int n = a.length();
 
  int dp[][] = new int[n + 1][2];
 
  // Initial state of the dp
  // dp[0][0] will be 1 if
  // the current bit is 1
  // and we have to flip it
  if(a.charAt(0) == '1')
  {
    dp[0][0] = 1 ;
  }
  else dp[0][0] = 0;
 
  // Initial state of the dp
  // dp[0][1] will be 1 if
  // the current bit is 0
  // and we have to flip it
  if(a.charAt(0) == '0')
    dp[0][1] = 1;
  else dp[0][1] = 0;
 
  for (int i = 1; i < n; i++)
  {
    // dp[i][0] = Flips required to
    // make all previous bits zero
    // Flip required to make current
    // bit zero
    if(a.charAt(i) == '1')
    {
      dp[i][0] = dp[i - 1][0] + 1;
    }
    else dp[i][0] = dp[i - 1][0];
 
    // dp[i][1] = mimimum flips
    // required to make all previous
    // states 0 or make previous states
    // 1 satisfying the condition
    if(a.charAt(i) == '0')
    {
      dp[i][1] = Math.min(dp[i - 1][0],
                          dp[i - 1][1]) + 1;
    }
    else dp[i][1] = Math.min(dp[i - 1][0],
                             dp[i - 1][1]);
  }
 
  int answer = Integer.MAX_VALUE;
  for (int i = 0; i < n; i++)
  {
    answer = Math.min(answer, dp[i][1] +
                      dp[n - 1][0] -
                      dp[i][0]);
  }
 
  //Minimum of answer and flips
  //required to make all bits 0
  return Math.min(answer,
                  dp[n - 1][0]);
}
   
// Driver code
public static void main (String[] args)
{
  String s = "1100111000101";
  System.out.println(minFlip(s));
}
}
 
// This code is contributed by satvikshrivas26


Python3
# Python implementation for Minimum number of
# flips required in a binary string such
# that all the 1’s are together
 
def minFlip(a):
     
     # Length of the binary string
    n = len(a)
     
    dp =[[0, 0] for i in range(n)]
     
    # Initial state of the dp
    # dp[0][0] will be 1 if the current
    # bit is 1 and we have to flip it
    dp[0][0]= int(a[0]=='1')
     
    # Initial state of the dp
    # dp[0][1] will be 1 if the current
    # bit is 0 and we have to flip it
    dp[0][1]= int(a[0]=='0')
     
 
    for i in range(1, n):
         
         
        # dp[i][0] = Flips required to
        # make all previous bits zero
        # + Flip required to make current bit zero
        dp[i][0]= dp[i-1][0]+int(a[i]=='1')
         
         
        # dp[i][1] = mimimum flips required
        # to make all previous states 0 or make
        # previous states 1 satisfying the condition
        dp[i][1]= min(dp[i-1])+int(a[i]=='0')
         
     
 
    answer = 10**18
     
    for i in range(n):
        answer = min(answer,
                     dp[i][1]+dp[n-1][0]-dp[i][0])
     
    # Minimum of answer and flips
    # required to make all bits 0
    return min(answer, dp[n-1][0])
     
 
# Driver code
s = "1100111000101"
 
print(minFlip(s))


C#
// C# implementation for
// Minimum number of
// flips required in
// a binary string such
// that all the 1’s are together
using System;
class GFG{
  
static int minFlip(string a)
{
  //Length of the binary string
  int n = a.Length;
 
  int [,]dp=new int[n + 1, 2];
 
  //Initial state of the dp
  //dp[0][0] will be 1 if the current
  //bit is 1 and we have to flip it
  dp[0, 0] = (a[0] == '1' ? 1 : 0);
 
  //Initial state of the dp
  //dp[0][1] will be 1 if the current
  //bit is 0 and we have to flip it
  dp[0, 1] = (a[0] == '0' ? 1 : 0);
 
  for (int i = 1; i < n; i++)
  {
    //dp[i][0] = Flips required to
    //make all previous bits zero
    //+ Flip required to make current bit zero
    dp[i, 0] = dp[i - 1, 0] +
                 (a[i] == '1' ? 1 : 0);
 
    //dp[i][1] = mimimum flips required
    //to make all previous states 0 or make
    //previous states 1 satisfying the condition
    dp[i, 1] = Math.Min(dp[i - 1, 0],
                        dp[i - 1, 1]) +
                       (a[i] == '0' ? 1 : 0);
 
  }
  int answer = int.MaxValue;
   
  for (int i = 0; i < n; i++)
  {
    answer = Math.Min(answer, dp[i, 1] +
                      dp[n - 1, 0] - dp[i, 0]);
  }
 
  //Minimum of answer and flips
  //required to make all bits 0
  return Math.Min(answer, dp[n - 1, 0]);
}
  
// Driver code
public static void Main(string[] args)
{
  string s = "1100111000101";
  Console.Write(minFlip(s));
}
}
 
// This code is contributed by Rutvik_56


Javascript


输出:
4

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程