📌  相关文章
📜  使右侧全为 1,左侧全为 0 的最小翻转次数

📅  最后修改于: 2021-09-02 06:35:29             🧑  作者: Mango

给定二进制字符串str ,任务是找到需要翻转的最小字符数,以使右侧全部为1 ,左侧全部为0

例子:

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

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

下面是上述方法的实现:

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


Javascript


输出
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live