📜  最多移除一个空细胞后,非空细胞的最长子阵列

📅  最后修改于: 2021-05-14 06:09:43             🧑  作者: Mango

给定一个二进制数组arr [] ,任务是在最多删除1个空单元之后找到非空单元的最长子数组。

例子:

方法:
这个想法是将1的频率存储在每个索引的前缀和后缀中,以计算特定索引在两个方向上最长的连续1序列。请按照以下步骤解决问题:

  • 初始化两个数组l []r [] ,它们分别从数组的左侧和右侧开始存储数组arr []中最长连续1的长度。
  • 遍历索引(0,N)的输入数组并为每个arr [i] = 1计数增加1 。否则,存储count的值,直到将l [i]中的(i – 1)索引重置为零为止。
  • 同样,通过遍历索引[N – 1,0]重复上述步骤,将从右开始的计数存储在r []中
  • 对于每个i其中包含0索引指标,通过除去该0,它等于L [I] + R [I]计算非空子阵列可能的长度。
  • 计算所有此类长度的最大值并打印结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum length
// of a subarray of 1s after removing
// at most one 0
int longestSubarray(int a[], int n)
{
    // Stores the count of consecutive
    // 1's from left
    int l[n];
 
    // Stores the count of consecutive
    // 1's from right
    int r[n];
 
    // Traverse left to right
    for (int i = 0, count = 0;
        i < n; i++) {
 
        // If cell is non-empty
        if (a[i] == 1)
 
            // Increase count
            count++;
 
        // If cell is empty
        else {
 
            // Store the count of
            // consecutive 1's
            // till (i - 1)-th index
            l[i] = count;
            count = 0;
        }
    }
 
    // Traverse from right to left
    for (int i = n - 1, count = 0;
        i >= 0; i--) {
 
        if (a[i] == 1)
            count++;
 
        else {
 
            // Store the count of
            // consecutive 1s
            // till (i + 1)-th index
            r[i] = count;
            count = 0;
        }
    }
 
    // Stores the length of
    // longest subarray
    int ans = -1;
    for (int i = 0; i < n; ++i) {
 
        if (a[i] == 0)
 
            // Store the maximum
            ans = max(ans, l[i] + r[i]);
    }
 
    // If array a contains only 1s
    // return n else return ans
    return ans < 0 ? n : ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 1, 1, 1, 0, 1,
                0, 1, 1 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << longestSubarray(arr, n);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to find the maximum length
// of a subarray of 1s after removing
// at most one 0
public static int longestSubarray(int[] a,
                                int n)
{
     
    // Stores the count of consecutive
    // 1's from left
    int[] l = new int[n];
     
    // Stores the count of consecutive
    // 1's from right
    int[] r = new int[n];
     
    // Traverse left to right
    for(int i = 0, count = 0;
            i < n; i++)
    {
         
    // If cell is non-empty
    if (a[i] == 1)
         
        // Increase count
        count++;
         
    // If cell is empty
    else
    {
             
        // Store the count of
        // consecutive 1's
        // till (i - 1)-th index
        l[i] = count;
        count = 0;
    }
    }
     
    // Traverse from right to left
    for(int i = n - 1, count = 0;
            i >= 0; i--)
    {
    if (a[i] == 1)
        count++;
         
    else
    {
             
        // Store the count of
        // consecutive 1s
        // till (i + 1)-th index
        r[i] = count;
        count = 0;
        }
    }
     
    // Stores the length of
    // longest subarray
    int ans = -1;
    for(int i = 0; i < n; ++i)
    {
    if (a[i] == 0)
             
        // Store the maximum
        ans = Math.max(ans, l[i] + r[i]);
    }
     
    // If array a contains only 1s
    // return n else return ans
    return ans < 0 ? n : ans;
}
 
// Driver code
public static void main(String[] args)
{
    int[] arr = { 0, 1, 1, 1, 0,
                1, 0, 1, 1 };
    int n = arr.length;
     
    System.out.println(longestSubarray(arr, n));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
 
# Function to find the maximum length
# of a subarray of 1s after removing
# at most one 0
def longestSubarray(a, n):
 
    # Stores the count of consecutive
    # 1's from left
    l = [0] * (n)
 
    # Stores the count of consecutive
    # 1's from right
    r = [0] * (n)
     
    count = 0
     
    # Traverse left to right
    for i in range(n):
         
        # If cell is non-empty
        if (a[i] == 1):
             
            # Increase count
            count += 1
         
        # If cell is empty
        else:
             
            # Store the count of
            # consecutive 1's
            # till (i - 1)-th index
            l[i] = count
            count = 0
     
    count = 0
    # Traverse from right to left
    for i in range(n - 1, -1, -1):
        if (a[i] == 1):
            count += 1
             
        else:
             
            # Store the count of
            # consecutive 1s
            # till (i + 1)-th index
            r[i] = count
            count = 0
     
    # Stores the length of
    # longest subarray
    ans = -1
    for i in range(n):
        if (a[i] == 0):
             
            # Store the maximum
            ans = max(ans, l[i] + r[i])
     
    # If array a contains only 1s
    # return n else return ans
    return ans < 0 and n or ans
 
# Driver code
arr = [ 0, 1, 1, 1, 0, 1, 0, 1, 1 ]
 
n = len(arr)
 
print(longestSubarray(arr, n))
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the maximum length
// of a subarray of 1s after removing
// at most one 0
public static int longestSubarray(int[] a,
                                  int n)
{
     
    // Stores the count of consecutive
    // 1's from left
    int[] l = new int[n];
     
    // Stores the count of consecutive
    // 1's from right
    int[] r = new int[n];
     
    // Traverse left to right
    for(int i = 0, count = 0; i < n; i++)
    {
         
        // If cell is non-empty
        if (a[i] == 1)
             
            // Increase count
            count++;
             
        // If cell is empty
        else
        {
             
            // Store the count of
            // consecutive 1's
            // till (i - 1)-th index
            l[i] = count;
            count = 0;
        }
    }
     
    // Traverse from right to left
    for(int i = n - 1, count = 0;
            i >= 0; i--)
    {
    if (a[i] == 1)
        count++;
         
    else
    {
             
        // Store the count of
        // consecutive 1s
        // till (i + 1)-th index
        r[i] = count;
        count = 0;
        }
    }
     
    // Stores the length of
    // longest subarray
    int ans = -1;
    for(int i = 0; i < n; ++i)
    {
        if (a[i] == 0)
                 
            // Store the maximum
            ans = Math.Max(ans, l[i] + r[i]);
    }
     
    // If array a contains only 1s
    // return n else return ans
    return ans < 0 ? n : ans;
}
 
 
// Driver code
public static void Main()
{
    int[] arr = { 0, 1, 1, 1, 0,
                  1, 0, 1, 1 };
    int n = arr.Length;
 
    Console.Write(longestSubarray(arr, n));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
4

时间复杂度: O(N)
辅助空间: O(N)