📌  相关文章
📜  删除最多一个元素后严格增加子阵列的最大长度

📅  最后修改于: 2021-04-27 17:50:13             🧑  作者: Mango

给定一个数组arr [] ,任务是删除最多一个元素并计算严格增加的子数组的最大长度。

例子:

方法:

  • 创建两个大小为N的数组pre []pos []
  • 从(0,N)遍历输入数组arr []以找出当前元素arr [i]在数组中的贡献,直到现在[0,i)并更新pre []数组(如果它对严格意义上的贡献)增加子数组。
  • 从[N – 2,0]遍历输入数组arr []以找出当前元素arr [j]到现在(N,j)在数组中的贡献,如果arr [j则更新pos []数组]贡献了最长的增长子数组。
  • 在不删除任何元素的情况下,计算严格增加的子数组的最大长度。
  • 遍历数组pre []pos []以通过排除该元素来找出当前元素的贡献。
  • 维护变量ans以找到到目前为止找到的最大值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the maximum length of
// strictly increasing subarray after
// removing atmost one element
int maxIncSubarr(int a[], int n)
{
    // Create two arrays pre and pos
    int pre[n] = { 0 };
    int pos[n] = { 0 };
    pre[0] = 1;
    pos[n - 1] = 1;
    int l = 0;
 
    // Find out the contribution of the current
    // element in array[0, i] and update pre[i]
    for (int i = 1; i < n; i++) {
        if (a[i] > a[i - 1])
            pre[i] = pre[i - 1] + 1;
        else
            pre[i] = 1;
    }
 
    // Find out the contribution of the current
    // element in array[N - 1, i] and update pos[i]
    l = 1;
    for (int i = n - 2; i >= 0; i--) {
        if (a[i] < a[i + 1])
            pos[i] = pos[i + 1] + 1;
        else
            pos[i] = 1;
    }
 
    // Calculate the maximum length of the
    // stricly increasing subarray without
    // removing any element
    int ans = 0;
    l = 1;
    for (int i = 1; i < n; i++) {
        if (a[i] > a[i - 1])
            l++;
        else
            l = 1;
        ans = max(ans, l);
    }
 
    // Calculate the maximum length of the
    // strictly increasing subarray after
    // removing the current element
    for (int i = 1; i <= n - 2; i++) {
        if (a[i - 1] < a[i + 1])
            ans = max(pre[i - 1] + pos[i + 1], ans);
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2 };
    int n = sizeof(arr) / sizeof(int);
 
    cout << maxIncSubarr(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function to return the maximum length of
    // strictly increasing subarray after
    // removing atmost one element
    static int maxIncSubarr(int a[], int n)
    {
        // Create two arrays pre and pos
        int pre[] = new int[n] ;
        int pos[] = new int[n] ;
        pre[0] = 1;
        pos[n - 1] = 1;
        int l = 0;
     
        // Find out the contribution of the current
        // element in array[0, i] and update pre[i]
        for (int i = 1; i < n; i++)
        {
            if (a[i] > a[i - 1])
                pre[i] = pre[i - 1] + 1;
            else
                pre[i] = 1;
        }
     
        // Find out the contribution of the current
        // element in array[N - 1, i] and update pos[i]
        l = 1;
        for (int i = n - 2; i >= 0; i--)
        {
            if (a[i] < a[i + 1])
                pos[i] = pos[i + 1] + 1;
            else
                pos[i] = 1;
        }
     
        // Calculate the maximum length of the
        // stricly increasing subarray without
        // removing any element
        int ans = 0;
        l = 1;
        for (int i = 1; i < n; i++)
        {
            if (a[i] > a[i - 1])
                l++;
            else
                l = 1;
            ans = Math.max(ans, l);
        }
     
        // Calculate the maximum length of the
        // strictly increasing subarray after
        // removing the current element
        for (int i = 1; i <= n - 2; i++)
        {
            if (a[i - 1] < a[i + 1])
                ans = Math.max(pre[i - 1] +
                                pos[i + 1], ans);
        }
        return ans;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = {1, 2};
        int n = arr.length;
     
        System.out.println(maxIncSubarr(arr, n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python implementation of the approach
 
# Function to return the maximum length of
# strictly increasing subarray after
# removing atmost one element
def maxIncSubarr(a, n):
     
    # Create two arrays pre and pos
    pre = [0] * n;
    pos = [0] * n;
    pre[0] = 1;
    pos[n - 1] = 1;
    l = 0;
 
    # Find out the contribution of the current
    # element in array[0, i] and update pre[i]
    for i in range(1, n):
        if (a[i] > a[i - 1]):
            pre[i] = pre[i - 1] + 1;
        else:
            pre[i] = 1;
     
    # Find out the contribution of the current
    # element in array[N - 1, i] and update pos[i]
    l = 1;
    for i in range(n - 2, -1, -1):
        if (a[i] < a[i + 1]):
            pos[i] = pos[i + 1] + 1;
        else:
            pos[i] = 1;
     
    # Calculate the maximum length of the
    # stricly increasing subarray without
    # removing any element
    ans = 0;
    l = 1;
    for i in range(1, n):
        if (a[i] > a[i - 1]):
            l += 1;
        else:
            l = 1;
        ans = max(ans, l);
     
    # Calculate the maximum length of the
    # strictly increasing subarray after
    # removing the current element
    for i in range(1, n - 1):
        if (a[i - 1] < a[i + 1]):
            ans = max(pre[i - 1] + pos[i + 1], ans);
     
    return ans;
 
# Driver code
if __name__ == '__main__':
    arr = [ 1, 2 ];
    n = len(arr);
 
    print(maxIncSubarr(arr, n));
     
# This code is contributed by PrinciRaj1992


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the maximum length of
    // strictly increasing subarray after
    // removing atmost one element
    static int maxIncSubarr(int []a, int n)
    {
        // Create two arrays pre and pos
        int []pre = new int[n] ;
        int []pos = new int[n] ;
        pre[0] = 1;
        pos[n - 1] = 1;
        int l = 0;
     
        // Find out the contribution of the current
        // element in array[0, i] and update pre[i]
        for (int i = 1; i < n; i++)
        {
            if (a[i] > a[i - 1])
                pre[i] = pre[i - 1] + 1;
            else
                pre[i] = 1;
        }
     
        // Find out the contribution of the current
        // element in array[N - 1, i] and update pos[i]
        l = 1;
        for (int i = n - 2; i >= 0; i--)
        {
            if (a[i] < a[i + 1])
                pos[i] = pos[i + 1] + 1;
            else
                pos[i] = 1;
        }
     
        // Calculate the maximum length of the
        // stricly increasing subarray without
        // removing any element
        int ans = 0;
        l = 1;
        for (int i = 1; i < n; i++)
        {
            if (a[i] > a[i - 1])
                l++;
            else
                l = 1;
            ans = Math.Max(ans, l);
        }
     
        // Calculate the maximum length of the
        // strictly increasing subarray after
        // removing the current element
        for (int i = 1; i <= n - 2; i++)
        {
            if (a[i - 1] < a[i + 1])
                ans = Math.Max(pre[i - 1] +
                                pos[i + 1], ans);
        }
        return ans;
    }
     
    // Driver code
    public static void Main()
    {
        int []arr = {1, 2};
        int n = arr.Length;
     
        Console.WriteLine(maxIncSubarr(arr, n));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
2

时间复杂度: O(N)

空间复杂度: O(N)