📜  允许一次更改的最长递增子数组

📅  最后修改于: 2022-05-13 01:57:52.689000             🧑  作者: Mango

允许一次更改的最长递增子数组

给定一个数组,找到最长递增子数组(连续元素)的长度,使得序列中最多可以更改一个数字(将一个数字更改为您想要的任何整数),以使序列严格递增。

例子:

Input  : 6
         7 2 3 1 5 10 
Output : 5
Explanation : 
Here, we can choose subarray 2, 3, 1, 5, 10 
and by changing its 3rd element (that is 1) 
to 4, it will become increasing sequence.

Input  : 2
         10 10
Output : 2
Explanation : 
Here, we can choose subarray 10, 10 and by
changing its 2nd element (that is 10) to 11,
it will become increasing sequence.

方法 :

第 1 步:我们首先为给定数组中的每个索引计算以索引结束的最长递增子数组。我们将这些值存储在 l[] 中。
第 2 步:然后为给定数组中的每个索引计算从索引开始的最长递增子数组。我们将这些值存储在 r[] 中。
第 3 步:当 a[i-1] + 1 < a[i+1] 时,更新答案 ans = max ( ans, l[i-1] + r[i+1] + 1)。

下面是上述思想的实现:

C++
// CPP program to find longest increasing subarray
// with one change allowed.
#include 
using namespace std;
 
// Function to find length of
// subsequence
int seg(int a[], int n)
{
    int l[n], r[n + 1], ans = 0;
 
    // calculating the l array.
    l[0] = 1;
    for (int i = 1; i < n; i++)
    {
        if (a[i] > a[i - 1])
        {
            l[i] = l[i - 1] + 1;
            ans = max(ans, l[i]);
        }
        else
            l[i] = 1;
    }
    if (ans != n)
        ++ans;
 
    // calculating the r array.
    r[n] = 0;
    for (int i = n - 1; i >= 0; i--)
    {
        if (a[i] < a[i + 1])
            r[i] = r[i + 1] + 1;
        else
            r[i] = 1;
    }
 
    // updating the answer.
    for (int i = n - 2; i > 0; i--)
    {
        if (a[i + 1] - a[i - 1] > 1)
            ans = max(ans, l[i - 1] + r[i + 1] + 1);
    }
 
    return max(ans, r[0]);
}
 
// Driver code.
int main()
{
    int a[] = { 9, 4, 5, 1, 13 };
    int n = sizeof(a) / sizeof(a[0]);
   
    // Function call
    cout << seg(a, n);
    return 0;
}


Java
// Java program to find longest increasing subarray
// with one change allowed.
class GFG
{
 
    // Function to find length of
    // subsequence
    static int seg(int[] a, int n)
    {
        int[] l = new int[n];
        int[] r = new int[n + 1];
        int ans = 0;
 
        // calculating the l array.
        l[0] = 1;
        for (int i = 1; i < n; i++)
        {
            if (a[i] > a[i - 1])
            {
                l[i] = l[i - 1] + 1;
                ans = Math.max(ans, l[i]);
            }
            else
                l[i] = 1;
        }
        if (ans != n)
            ++ans;
 
        // calculating the r array.
        r[n] = 0;
        for (int i = n - 1; i > 0; i--)
        {
            if (a[i - 1] < a[i])
                r[i] = r[i + 1] + 1;
            else
                r[i] = 1;
        }
 
        // updating the answer.
        for (int i = n - 2; i > 0; i--)
        {
            if (a[i + 1] - a[i - 1] > 1)
                ans = Math.max(ans,
                               l[i - 1] + r[i + 1] + 1);
        }
        return Math.max(ans, r[0]);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] a = { 9, 4, 5, 1, 13 };
        int n = a.length;
 
        // Function call
        System.out.println(seg(a, n));
    }
}
 
// This code is contributed by
// sanjeev2552


Python3
# Python3 program to find
# longest increasing subarray
# with one change allowed.
 
# Function to find length of
# subsequence
def seg(a, n):
 
    l = [0] * n
    r = [0] * (n + 1)
    ans = 0
 
    # calculating the l array.
    l[0] = 1
    for i in range(1, n):
        if (a[i] > a[i - 1]):
            l[i] = l[i - 1] + 1
            ans = max(ans, l[i])
        else:
            l[i] = 1
 
    if (ans != n):
        ans += 1
 
    # calculating the
    # r array.
    r[n] = 0
    for i in range(n - 1,
                   -1, -1):
        if (a[i - 1] < a[i]):
            r[i] = r[i + 1] + 1
        else:
            r[i] = 1
 
    # Updating the answer.
    for i in range(n - 2,
                   0, -1):
        if (a[i + 1] -
            a[i - 1] > 1):
            ans = max(ans, l[i - 1] +
                      r[i + 1] + 1)
 
    return max(ans, r[0])
 
# Driver code.
if __name__ == "__main__":
 
    a = [9, 4, 5, 1, 13]
    n = len(a)
 
    # Function call
    print(seg(a, n))
 
# This code is contributed by Chitranayal


C#
// C# program to find longest increasing subarray
// with one change allowed.
using System;
 
class GFG {
 
    // Function to find length of
    // subsequence
    static int seg(int[] a, int n)
    {
        int[] l = new int[n];
        int[] r = new int[n + 1];
        int ans = 0;
 
        // calculating the l array.
        l[0] = 1;
        for (int i = 1; i < n; i++)
        {
            if (a[i] > a[i - 1])
            {
                l[i] = l[i - 1] + 1;
                ans = Math.Max(ans, l[i]);
            }
            else
                l[i] = 1;
        }
        if (ans != n)
            ++ans;
 
        // calculating the r array.
        r[n] = 0;
        for (int i = n - 1; i > 0; i--)
        {
            if (a[i - 1] < a[i])
                r[i] = r[i + 1] + 1;
            else
                r[i] = 1;
        }
 
        // updating the answer.
        for (int i = n - 2; i > 0; i--)
        {
            if (a[i + 1] - a[i - 1] > 1)
                ans = Math.Max(ans,
                               l[i - 1] + r[i + 1] + 1);
        }
        return Math.Max(ans, r[0]);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int[] a = { 9, 4, 5, 1, 13 };
        int n = a.Length;
 
        // Function call
        Console.WriteLine(seg(a, n));
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出
4