📌  相关文章
📜  最多删除一个空单元后的最长非空单元子阵列

📅  最后修改于: 2021-10-26 02:35:10             🧑  作者: Mango

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

例子:

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

  • 初始化两个数组l[]r[] ,它们分别存储数组arr[] 中从数组左侧和右侧开始的最长连续1的长度。
  • 在索引(0, N) 上迭代输入数组并为每个arr[i] = 1增加 1计数。否则,将count的值存储到l[i] 中的(i – 1)索引将 count 重置为零。
  • 类似地,通过遍历索引[N – 1, 0]重复上述步骤,将计数从右侧存储在r[] 中
  • 对于包含0 的每个i索引索引,通过删除该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)