📌  相关文章
📜  最小翻转以使所有1都在右侧,而0则在左侧

📅  最后修改于: 2021-05-04 16:13:00             🧑  作者: Mango

给定一个二进制字符串str ,任务是找到要翻转为使全1 s在右侧和全0 s在左侧所需的最小字符数。

例子:

的方法:我们的想法是指望字符串的各指标的右侧的0秒的数量和在字符串中的每个索引的左侧计数的1秒的数量。请按照以下步骤解决问题:

  • 初始化一个变量,例如cntzero ,将0的总计数存储在给定的字符串。
  • 遍历该字符串并计算给定字符串中0的总数。
  • 如果给定字符串中的cntzero0或等于字符串的长度,则结果将为0
  • 遍历字符串,对于每个索引,找到索引左侧的1 s计数与索引右侧的0 s计数之和。
  • 从所有获得的总和中找到最小值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the minimum count
// of flips required to make all 1s on
// the right and all 0s on the left of
// the given string
int minimumCntOfFlipsRequired(string str)
{
    // Stores length of str
    int n = str.length();
 
    // Store count of 0s in the string
    int zeros = 0;
 
    // Traverse the string
    for (int i = 0; i < n; i++) {
 
        // If current character is 0
        if (str[i] == '0') {
 
            // Update zeros
            zeros++;
        }
    }
 
    // If count of 0s in the string
    // is 0 or n
    if (zeros == 0 || zeros == n) {
        return 0;
    }
 
    // Store minimum count of flips
    // required to make all 0s on
    // the left and all 1s on the right
    int minFlips = INT_MAX;
 
    // Stores count of 1s on the left
    // of each index
    int currOnes = 0;
 
    // Stores count of flips required to make
    // string monotonically increasing
    int flips;
 
    // Traverse the string
    for (int i = 0; i < n; i++) {
 
        // If current character
        // is 1
        if (str[i] == '1') {
 
            // Update currOnes
            currOnes++;
        }
 
        // Update flips
        flips = currOnes + (zeros - (i + 1 - currOnes));
 
        // Update the minimum
        // count of flips
        minFlips = min(minFlips, flips);
    }
 
    return minFlips;
}
 
// Driver Code
int main()
{
    string str = "100101";
    cout << minimumCntOfFlipsRequired(str);
    return 0;
}


Java
// Java program to implement
// the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to find the minimum count of flips
    // required to make all 1s on the right and
    // all 0s on the left of the given string
    public static int minimumCntOfFlipsRequired(
        String str)
    {
        // Stores length of str
        int n = str.length();
 
        // Store count of 0s in the string
        int zeros = 0;
 
        // Traverse the string
        for (int i = 0; i < n; i++) {
 
            // If current character is 0
            if (str.charAt(i) == '0') {
 
                // Update zeros
                zeros++;
            }
        }
 
        // If count of 0s in the string
        // is 0 or n
        if (zeros == 0 || zeros == n) {
            return 0;
        }
 
        // Store minimum count of flips
        // required to make all 0s on
        // the left and 1s on the right
        int minFlips = Integer.MAX_VALUE;
 
        // Stores count of 1s on the left
        // side of each index
        int currOnes = 0;
 
        // Stores count of flips required to make
        // all 0s on the left and 1s on the right
        int flips;
 
        // Traverse the string
        for (int i = 0; i < n; i++) {
 
            // If current character is 1
            if (str.charAt(i) == '1') {
 
                // Update currOnes
                currOnes++;
            }
 
            // Update flips
            flips = currOnes + (zeros - (i + 1 - currOnes));
 
            // Update the minimum
            // count of flips
            minFlips = Math.min(minFlips, flips);
        }
 
        return minFlips;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s1 = "100101";
        System.out.println(minimumCntOfFlipsRequired(s1));
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to find the minimum count
# of flips required to make all 1s on
# the right and all 0s on the left of
# the given string
def minimumCntOfFlipsRequired(str):
     
    # Stores length of str
    n = len(str);
 
 
    # Store count of 0s in the string
    zeros = 0;
 
 
    # Traverse the string
    for i in range(n):
         
         
        # If current character
        # is 0
        if (str[i] == '0'):
             
             
            # Update zeros
            zeros += 1;
 
 
    # If count of 0s in the string
    # is 0 or n
    if (zeros == 0 or zeros == n):
        return 0;
 
 
    # Store minimum count of flips
    # required to make all 0s on the
    # left and all 1s on the right
    minFlips = 10000001;
 
 
    # Stores count of 1s on the left
    # of each index
    currOnes = 0;
     
     
    # Stores count of flips required to make
    # all 0s on the left and all 1s on the right
    flips = 0;
 
 
    # Traverse the string
    for i in range(n):
 
        # If current character is 1
        if (str[i] == '1'):
 
            # Update currOnes
            currOnes += 1;
 
        # Update flips
        flips = currOnes + (zeros - (i + 1 - currOnes));
 
 
        # Update the minimum
        # count of flips
        minFlips = min(minFlips, flips);
 
    return minFlips;
 
 
 
# Driver Code
if __name__ == '__main__':
    str = "100101";
    print(minimumCntOfFlipsRequired(str));


C#
// C# program to implement
// the above approach 
using System;
  
class GFG{
  
// Function to find the minimum count of flips
// required to make all 1s on the right and
// all 0s on the left of the given string
public static int minimumCntOfFlipsRequired(string str)
{
     
    // Stores length of str
    int n = str.Length;
     
    // Store count of 0s in the string
    int zeros = 0;
 
    // Traverse the string
    for(int i = 0; i < n; i++)
    {
         
        // If current character is 0
        if (str[i] == '0')
        {
             
            // Update zeros
            zeros++;
        }
    }
 
    // If count of 0s in the string
    // is 0 or n
    if (zeros == 0 || zeros == n)
    {
        return 0;
    }
 
    // Store minimum count of flips
    // required to make all 0s on
    // the left and 1s on the right
    int minFlips = Int32.MaxValue;
 
    // Stores count of 1s on the left
    // side of each index
    int currOnes = 0;
 
    // Stores count of flips required
    // to make all 0s on the left and
    // 1s on the right
    int flips;
 
    // Traverse the string
    for(int i = 0; i < n; i++)
    {
         
        // If current character is 1
        if (str[i] == '1')
        {
             
            // Update currOnes
            currOnes++;
        }
 
        // Update flips
        flips = currOnes +
             (zeros - (i + 1 - currOnes));
 
        // Update the minimum
        // count of flips
        minFlips = Math.Min(minFlips, flips);
    }
    return minFlips;
}
 
// Driver code
public static void Main()
{
    string s1 = "100101";
     
    Console.WriteLine(minimumCntOfFlipsRequired(s1));
}
}
 
// This code is contributed by sanjoy_62


输出
2




时间复杂度: O(N),其中N是字符串的长度
辅助空间: O(1)