Dada array binaria. La tarea es encontrar la longitud del subarreglo con un número mínimo de 1s.
Nota : se garantiza que hay al menos un 1 presente en la array.
Ejemplos :
Entrada : arr[] = {1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1}
Salida : 3
El subarreglo de longitud mínima de 1s es {1, 1}.
Entrada : arr[] = {0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}
Salida : 1
Solución simple : una solución simple es considerar cada subarreglo y contar los 1 en cada subarreglo. Finalmente, devuelva el tamaño de retorno del subarreglo de longitud mínima de 1s.
Solución eficiente : una solución eficiente es atravesar la array de izquierda a derecha. Si vemos un 1, incrementamos la cuenta. Si vemos un 0, y el conteo de 1s hasta ahora es positivo, calcule el mínimo del conteo y el resultado y restablezca el conteo a cero.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to count minimum length // subarray of 1's in a binary array. #include <bits/stdc++.h> using namespace std; // Function to count minimum length subarray // of 1's in binary array arr[0..n-1] int getMinLength(bool arr[], int n) { int count = 0; // initialize count int result = INT_MAX; // initialize result for (int i = 0; i < n; i++) { if (arr[i] == 1) { count++; } else { if (count != 0) result = min(result, count); count = 0; } } return result; } // Driver code int main() { bool arr[] = { 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1 }; int n = sizeof(arr) / sizeof(arr[0]); cout << getMinLength(arr, n) << endl; return 0; }
Java
// Java program to count minimum length // subarray of 1's in a binary array. import java.io.*; class GFG { // Function to count minimum length subarray // of 1's in binary array arr[0..n-1] static int getMinLength(double arr[], int n) { int count = 0; // initialize count int result = Integer.MAX_VALUE; // initialize result for (int i = 0; i < n; i++) { if (arr[i] == 1) { count++; } else { if (count != 0) result = Math.min(result, count); count = 0; } } return result; } // Driver code public static void main (String[] args) { double arr[] = { 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1 }; int n = arr.length; System.out.println (getMinLength(arr, n)); } } // This code is contributed by ajit.
Python3
# Python program to count minimum length # subarray of 1's in a binary array. import sys # Function to count minimum length subarray # of 1's in binary array arr[0..n-1] def getMinLength(arr, n): count = 0; # initialize count result = sys.maxsize ; # initialize result for i in range(n): if (arr[i] == 1): count+=1; else: if(count != 0): result = min(result, count); count = 0; return result; # Driver code arr = [ 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1 ]; n = len(arr); print(getMinLength(arr, n)); # This code is contributed by Rajput-Ji
C#
// C# program to count minimum length // subarray of 1's in a binary array. using System; class GFG { // Function to count minimum length subarray // of 1's in binary array arr[0..n-1] static int getMinLength(double []arr, int n) { int count = 0; // initialize count int result = int.MaxValue; // initialize result for (int i = 0; i < n; i++) { if (arr[i] == 1) { count++; } else { if (count != 0) result = Math.Min(result, count); count = 0; } } return result; } // Driver code static public void Main () { double []arr = { 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1 }; int n = arr.Length; Console.WriteLine(getMinLength(arr, n)); } } // This code is contributed by Tushil..
Javascript
<script> // javascript program to count minimum length // subarray of 1's in a binary array. // Function to count minimum length subarray // of 1's in binary array arr[0..n-1] function getMinLength(arr, n) { // initialize count var count = 0; // initialize result var result = Number.MAX_VALUE; for (i = 0; i < n; i++) { if (arr[i] == 1) { count++; } else { if (count != 0) result = Math.min(result, count); count = 0; } } return result; } // Driver code var arr = [ 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1 ]; var n = arr.length; document.write(getMinLength(arr, n)); // This code is contributed by Amit Katiyar </script>
2
Complejidad de Tiempo : O(N)
Espacio Auxiliar : O(1)