Dada la string binaria str . En una sola operación, podemos cambiar cualquier ‘1’ a ‘0’ o cualquier ‘0’ a ‘1’ . La tarea es hacer un número mínimo de cambios en la string, de modo que si tomamos cualquier prefijo de la string, el número de 1 debe ser mayor o igual que el número de 0 .
Ejemplos:
Entrada: str = “10001”
Salida: 1
Podemos cambiar str[2] de ‘0’ a ‘1’.
Entrada: str = “0000”
Salida: 2
Enfoque: El problema se puede resolver con avidez. El primer carácter de la string tiene que ser 1 . Luego, para el resto de la string, recorremos la string carácter por carácter y verificamos si la condición requerida se cumple o no, si no, aumentamos la cantidad de cambios requeridos.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the minimum // changes required int minChanges(string str, int n) { // To store the count of minimum changes, // number of ones and the number of zeroes int count = 0, zeros = 0, ones = 0; // First character has to be '1' if (str[0] != '1') { count++; ones++; } for (int i = 1; i < n; i++) { if (str[i] == '0') zeros++; else ones++; // If condition fails // changes need to be made if (zeros > ones) { zeros--; ones++; count++; } } // Return the required count return count; } // Driver code int main() { string str = "0000"; int n = str.length(); cout << minChanges(str, n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the minimum // changes required static int minChanges(char[] str, int n) { // To store the count of minimum changes, // number of ones and the number of zeroes int count = 0, zeros = 0, ones = 0; // First character has to be '1' if (str[0] != '1') { count++; ones++; } for (int i = 1; i < n; i++) { if (str[i] == '0') zeros++; else ones++; // If condition fails // changes need to be made if (zeros > ones) { zeros--; ones++; count++; } } // Return the required count return count; } // Driver code public static void main(String[] args) { char []str = "0000".toCharArray(); int n = str.length; System.out.print(minChanges(str, n)); } } // This code has been contributed by 29AjayKumar
Python3
# Python3 implementation of the approach # Function to return the minimum # changes required def minChanges(str, n): # To store the count of minimum changes, # number of ones and the number of zeroes count, zeros, ones = 0, 0, 0 # First character has to be '1' if (ord(str[0])!= ord('1')): count += 1 ones += 1 for i in range(1, n): if (ord(str[i]) == ord('0')): zeros += 1 else: ones += 1 # If condition fails # changes need to be made if (zeros > ones): zeros -= 1 ones += 1 count += 1 # Return the required count return count # Driver code if __name__ == '__main__': str = "0000" n = len(str) print(minChanges(str, n)) # This code contributed by PrinciRaj1992
C#
// C# implementation of the approach using System; class GFG { // Function to return the minimum // changes required static int minChanges(char[] str, int n) { // To store the count of minimum changes, // number of ones and the number of zeroes int count = 0, zeros = 0, ones = 0; // First character has to be '1' if (str[0] != '1') { count++; ones++; } for (int i = 1; i < n; i++) { if (str[i] == '0') zeros++; else ones++; // If condition fails // changes need to be made if (zeros > ones) { zeros--; ones++; count++; } } // Return the required count return count; } // Driver code public static void Main(String[] args) { char []str = "0000".ToCharArray(); int n = str.Length; Console.Write(minChanges(str, n)); } } // This code contributed by Rajput-Ji
PHP
<?php // PHP implementation of the approach // Function to return the minimum // changes required function minChanges($str, $n) { // To store the count of minimum changes, // number of ones and the number of zeroes $count = $zeros = $ones = 0; // First character has to be '1' if ($str[0] != '1') { $count++; $ones++; } for ($i = 1; $i < $n; $i++) { if ($str[$i] == '0') $zeros++; else $ones++; // If condition fails // changes need to be made if ($zeros > $ones) { $zeros--; $ones++; $count++; } } // Return the required count return $count; } // Driver code $str = "0000"; $n = strlen($str); echo minChanges($str, $n); // This code is contributed by mits ?>
Javascript
<script> // Javascript implementation of the approach // Function to return the minimum // changes required function minChanges(str, n) { // To store the count of minimum changes, // number of ones and the number of zeroes let count = 0, zeros = 0, ones = 0; // First character has to be '1' if (str[0] != '1') { count++; ones++; } for (let i = 1; i < n; i++) { if (str[i] == '0') zeros++; else ones++; // If condition fails // changes need to be made if (zeros > ones) { zeros--; ones++; count++; } } // Return the required count return count; } // Driver Code let str = "0000".split(''); let n = str.length; document.write(minChanges(str, n)); </script>
2
Complejidad de tiempo: O(n), donde n es el tamaño de la string dada
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Sahil Srivastava y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA