📌  相关文章
📜  使二进制字符串增加的最小翻转次数

📅  最后修改于: 2022-05-13 01:57:06.434000             🧑  作者: Mango

使二进制字符串增加的最小翻转次数

给定一个二进制字符串S ,任务是找到需要翻转的最小字符数以使给定的二进制字符串增加。

例子:

方法:给定的问题可以通过使用贪心算法来解决,该算法基于以下观察结果:在任意数量的翻转之后,结果单调递增的字符串将是形式('0'*p + '1'*q) ,其中pq分别是修改后的字符串中 0 和 1 的计数。这个想法是遍历给定的字符串S并且对于每个索引,将子字符串S[0, i)修改为0s并将子字符串S[i, N)修改为1s并找到相应的最小翻转次数。请按照以下步骤解决问题:

  • 在给定的二进制字符串S中查找0的计数并将其存储在变量countZero中。
  • 初始化变量,例如minFlips(N – cntZero) ,它存储所需的最小翻转次数。
  • 初始化变量,例如cntOne0 ,它在遍历字符串时存储字符串中1的计数。
  • 遍历给定的字符串S并执行以下步骤:
    • 如果字符S[i]0 ,则将countZero的值减1
    • 否则,将minFlips的值更新为minFlips(countZero + countOne)的最小值,并将countOne的值增加1
  • 完成上述步骤后,打印minFlips的值作为结果。

下面是上述方法的实现。

C++
// C++ program for the above approach;
 
#include 
using namespace std;
 
// Function to find the minimum number of
// flips required to make string increasing
int minimumFlips(string s)
{
 
  // Length of s
  int n = s.size();
 
  // Total number of zero in s
  int cnt0 = count(s.begin(), s.end(), '0');
 
  // Stores count of 1s till ith index
  int cnt1 = 0;
 
  // stores the minimum count of flip
  int res = n - cnt0;
 
  // Traverse the given string
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      cnt0 -= 1;
    }
 
    // Update the value of res
    // and count of 1s
    else if (s[i] == '1') {
      res = min(res, cnt1 + cnt0);
      cnt1++;
    }
  }
 
  // Return the minimum number
  // of flips
  return res;
}
 
// Driver code
int main()
{
 
  // Given string
  string S = "000110";
 
  // function call
  cout << minimumFlips(S);
  return 0;
}
 
// This code is contributed by parthmanchanda81


Java
// Java program for the above approach;
import java.util.*;
 
class GFG
{
 
    // Function to find the minimum number of
    // flips required to make String increasing
    static int minimumFlips(String s) {
 
        // Length of s
        int n = s.length();
 
        // Total number of zero in s
        int cnt0 = count(s, '0');
 
        // Stores count of 1s till ith index
        int cnt1 = 0;
 
        // stores the minimum count of flip
        int res = n - cnt0;
 
        // Traverse the given String
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == '0') {
                cnt0 -= 1;
            }
 
            // Update the value of res
            // and count of 1s
            else if (s.charAt(i) == '1') {
                res = Math.min(res, cnt1 + cnt0);
                cnt1++;
            }
        }
 
        // Return the minimum number
        // of flips
        return res;
    }
 
    private static int count(String s, char c) {
        int ans = 0;
        for (char i : s.toCharArray())
            if (c == i)
                ans++;
        return ans;
    }
 
    // Driver code
    public static void main(String[] args) {
 
        // Given String
        String S = "000110";
 
        // function call
        System.out.print(minimumFlips(S));
    }
}
 
// This code is contributed by Princi Singh


Python3
# Python program for the above approach
 
# Function to find the minimum number of
# flips required to make string increasing
def minimumFlips(s):
 
    # Length of s
    n = len(s)
 
    # Total number of zero in s
    cnt0 = s.count('0')
 
    # Stores count of 1s till ith index
    cnt1 = 0
 
    # Stores the minimum count of flips
    res = n - cnt0
 
    # Traverse the given string S
    for i in range(n):
 
        if s[i] == '0':
            cnt0 -= 1
 
        elif s[i] == '1':
 
            # Update the value of res
            # and count of 1s
            res = min(res, cnt1 + cnt0)
            cnt1 += 1
 
    # Return the minimum number
    # of flips
    return res
 
 
# Driver Code
S = '000110'
 
# Function Call
print(minimumFlips(S))


C#
using System;
 
public class GFG {
    // Function to find the minimum number of
    // flips required to make String increasing
    static int minimumFlips(String s)
    {
 
        // Length of s
        int n = s.Length;
 
        // Total number of zero in s
        int cnt0 = count(s, '0');
 
        // Stores count of 1s till ith index
        int cnt1 = 0;
 
        // stores the minimum count of flip
        int res = n - cnt0;
 
        // Traverse the given String
        for (int i = 0; i < n; i++) {
            if (s[i] == '0') {
                cnt0 -= 1;
            }
 
            // Update the value of res
            // and count of 1s
            else if (s[i] == '1') {
                res = Math.Min(res, cnt1 + cnt0);
                cnt1++;
            }
        }
 
        // Return the minimum number
        // of flips
        return res;
    }
 
    private static int count(String s, char c)
    {
        int ans = 0;
        for (int j = 0; j < s.Length; j++) {
            char i = s[j];
            if (c == i)
                ans++;
        }
        return ans;
    }
 
    // Driver code
    static public void Main()
    {
        // Given String
        String S = "000110";
 
        // function call
        Console.Write(minimumFlips(S));
    }
}
 
// This code is contributed by maddler.


Javascript


输出:
1

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