📌  相关文章
📜  使左侧全为 1,右侧全为 0 的最小翻转 |设置 2

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

使左侧全为 1,右侧全为 0 的最小翻转 |设置 2

给定一个二进制数组,我们可以将左侧的所有 1 和右侧的所有 0 翻转。计算使左侧所有 1 和右侧所有 0 所需的最小翻转。
例子 :

Input: 1011000  
Output: 1
1 flip is required to make it 1111000.

Input : 00001 
Output : 2
2 flips required to make it 10000.

我们在下面的帖子中讨论了基于位掩码的解决方案。使左侧全为 1,右侧全为 0 的最小翻转 |设置 1(使用位掩码)
它可以通过 O(N) 时间复杂度(其中 N – 位数)和 O(N) 额外内存来完成

  1. 计算从左到右移动时需要完成的“0”翻转次数以使所有“1”位。
  2. 计算在从右向左移动以使所有位为“0”时需要完成的“1”翻转次数。
  3. 遍历位之间的所有位置并从两个数组中找到'0'-翻转+'1'-翻转的最小和。

C++
// CPP program to find minimum flips required
// to make all 1s in left and 0s in right.
#include 
using namespace std;
 
int minimalFilps(string bits)
{
    int n = bits.length();
 
    // two arrays will keep the count for number
    // of 0s' and 1s' to be flipped while
    // traversing from left to right and right to
    // left respectively
    int flipsFromLeft[n];
    int flipsFromRight[n];
 
    // Fill flipsFromLeft[]
    int flips = 0;
    for (int i = 0; i < n; i++) {
        if (bits[i] == '0')
            flips++;        
        flipsFromLeft[i] = flips;
    }
 
    // Fill flipsFromRight[]
    flips = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (bits[i] == '1')
            flips++;        
        flipsFromRight[i] = flips;
    }
 
    // initialize minFlip to highest int value. If sum
    // of leftflip and rightFlip is smaller than minflips,
    // then update minFlips
    int minFlips = INT_MAX;
    for (int i = 1; i < n; i++) {
        if (flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips)
            minFlips = flipsFromLeft[i - 1] + flipsFromRight[i];
    }
 
    return minFlips;
}
 
// Driver code
int main()
{
    string bits = "00001";
    cout << minimalFilps(bits) << endl;
    return 0;
}


Java
// Java program to find minimum flips required
// to make all 1s in left and 0s in right.
import java.io.*;
 
class GFG
{
    static int minimalFilps(String bits)
    {
        int n = bits.length();
     
        // two arrays will keep the count
        // for number of 0s' and 1s' to be
        // flipped while traversing from
        // left to right and right to
        // left respectively
        int flipsFromLeft[] = new int[n];
        int flipsFromRight[] =new int[n] ;
     
        // Fill flipsFromLeft[]
        int flips = 0;
        for (int i = 0; i < n; i++)
        {
            if (bits.charAt(i) == '0')
                flips++;        
            flipsFromLeft[i] = flips;
        }
     
        // Fill flipsFromRight[]
        flips = 0;
        for (int i = n - 1; i >= 0; i--)
        {
            if (bits.charAt(i) == '1')
                flips++;        
            flipsFromRight[i] = flips;
        }
     
        // initialize minFlip to highest int value. If sum
        // of leftflip and rightFlip is smaller than minflips,
        // then update minFlips
        int minFlips = Integer.MAX_VALUE;
        for (int i = 1; i < n; i++)
        {
            if (flipsFromLeft[i - 1] + flipsFromRight[i]
                                              < minFlips)
                minFlips = flipsFromLeft[i - 1]
                           + flipsFromRight[i];
        }
     
        return minFlips;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        String bits = "00001";
        System.out.println(minimalFilps(bits));
         
    }
}
 
// This code is contributed by vt_m.


Python3
# Python 3 program to find minimum flips required
# to make all 1s in left and 0s in right.
import sys
 
def minimalFilps(bits):
    n = len(bits)
 
    # two arrays will keep the count for number
    # of 0s' and 1s' to be flipped while
    # traversing from left to right and right to
    # left respectively
    flipsFromLeft = [0 for i in range(n)]
    flipsFromRight = [0 for i in range(n)]
 
    # Fill flipsFromLeft[]
    flips = 0
    for i in range(0, n, 1):
        if (bits[i] == '0'):
            flips = flips + 1   
        flipsFromLeft[i] = flips
     
    # Fill flipsFromRight[]
    flips = 0
    i = n - 1
    while(i >= 0):
        if (bits[i] == '1'):
            flips = flips + 1
        i = i - 1
        flipsFromRight[i] = flips
     
    # initialize minFlip to highest int value.
    # If sum of leftflip and rightFlip is smaller
    # than minflips, then update minFlips
    minFlips = sys.maxsize
    for i in range(1, n, 1):
        if (flipsFromLeft[i - 1] +
            flipsFromRight[i] < minFlips):
            minFlips = (flipsFromLeft[i - 1] +
                        flipsFromRight[i])
     
    return minFlips
 
# Driver code
if __name__ == '__main__':
    bits = "00001"
    print(minimalFilps(bits))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find minimum flips required
// to make all 1s in left and 0s in right.
using System;
 
class GFG
{
    static int minimalFilps(String bits)
    {
        int n = bits.Length;
     
        // two arrays will keep the count
        // for number of 0s' and 1s' to be
        // flipped while traversing from
        // left to right and right to
        // left respectively
        int []flipsFromLeft = new int[n];
        int []flipsFromRight =new int[n] ;
     
        // Fill flipsFromLeft[]
        int flips = 0;
        for (int i = 0; i < n; i++)
        {
            if (bits[i] == '0')
                flips++;        
            flipsFromLeft[i] = flips;
        }
     
        // Fill flipsFromRight[]
        flips = 0;
        for (int i = n - 1; i >= 0; i--)
        {
            if (bits[i] == '1')
                flips++;        
            flipsFromRight[i] = flips;
        }
     
        // initialize minFlip to highest int value.
        // If sum of leftflip and rightFlip is smaller
        // than minflips, then update minFlips
        int minFlips = int.MaxValue;
        for (int i = 1; i < n; i++)
        {
            if (flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips)
            minFlips = flipsFromLeft[i - 1] + flipsFromRight[i];
        }
     
        return minFlips;
    }
     
    // Driver code
    public static void Main ()
    {
        string bits = "00001";
        Console.WriteLine(minimalFilps(bits));
         
    }
}
 
// This code is contributed by vt_m.


PHP
= 0; $i--)
    {
        if ($bits[$i] == '1')
            $flips++;        
        $flipsFromRight[$i] = $flips;
    }
 
    // initialize minFlip to
    // highest int value. If sum
    // of leftflip and rightFlip
    // is smaller than minflips,
    // then update minFlips
    $INT_MAX=2147483647;
    $minFlips = $INT_MAX;
    for ($i = 1; $i < $n; $i++)
    {
        if ($flipsFromLeft[$i - 1] +
            $flipsFromRight[$i] < $minFlips)
            $minFlips = $flipsFromLeft[$i - 1] +
                        $flipsFromRight[$i];
    }
 
    return $minFlips;
}
 
    // Driver Code
    $bits = "00001";
    echo minimalFilps($bits) ;
 
// This code is contributed by nitin mittal.
?>


Javascript


输出:

2