📌  相关文章
📜  去除任意一个元素得到的最大连续递减序列

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

给定一个由N 个整数组成的数组arr[] 。任务是找到从数组arr[] 中删除最多一个元素后可以导出的连续严格递减序列的长度。
例子

方法:

  • 创建两个数组, left[]存储从左到右递减序列的长度, right[]存储从右到左递减序列的长度。
  • 遍历给定的数组arr[]
  • 如果前一个元素( arr[i-1] )大于下一个元素( arr[i+1] ),则检查删除该元素是否会给出递减子序列的最大长度。
  • 更新递减子序列的最大长度。

下面是上述方法的实现:

C++
// C++ program to find maximum length
// of decreasing sequence by removing
// at most one element
#include 
using namespace std;
 
// Function to find the maximum length
int maxLength(int* a, int n)
{
    // Intialise maximum length to 1
    int maximum = 1;
 
    // Intialise left[] to find the
    // length of decreasing sequence
    // from left to right
    int left[n];
 
    // Intialise right[] to find the
    // length of decreasing sequence
    // from right to left
    int right[n];
 
    // Initially store 1 at each index of
    // left and right array
    for (int i = 0; i < n; i++) {
        left[i] = 1;
        right[i] = 1;
    }
 
    // Iterate over the array arr[] to
    // store length of decreasing
    // sequence that can be obtained
    // at every index in the right array
    for (int i = n - 2; i >= 0; i--) {
 
        if (a[i] > a[i + 1]) {
            right[i] = right[i + 1] + 1;
        }
 
        // Store the length of longest
        // continuous decreasing
        // sequence in maximum
        maximum = max(maximum, right[i]);
    }
 
    // Iterate over the array arr[] to
    // store length of decreasing
    // sequence that can be obtained
    // at every index in the left array
    for (int i = 1; i < n; i++) {
        if (a[i] < a[i - 1]) {
            left[i] = left[i - 1] + 1;
        }
    }
 
    if (n > 2) {
        // Check if we can obtain a
        // longer decreasing sequence
        // after removal of any element
        // from the array arr[] with
        // the help of left[] & right[]
        for (int i = 1; i < n - 1; i++) {
            if (a[i - 1] > a[i + 1]) {
                maximum = max(maximum,
                              left[i - 1] + right[i + 1]);
            }
        }
    }
 
    // Return maximum length of sequence
    return maximum;
}
 
// Driver code
int main()
{
    int arr[6] = { 8, 7, 3, 5, 2, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function calling
    cout << maxLength(arr, n) << endl;
    return 0;
}


Java
// Java program to find maximum length
// of decreasing sequence by removing
// at most one element
class GFG {
     
    // Function to find the maximum length
    static int maxLength(int []a, int n)
    {
        // Intialise maximum length to 1
        int maximum = 1;
     
        // Intialise left[] to find the
        // length of decreasing sequence
        // from left to right
        int left [] = new int[n];
     
        // Intialise right[] to find the
        // length of decreasing sequence
        // from right to left
        int right[] = new int[n];
     
        // Initially store 1 at each index of
        // left and right array
        for (int i = 0; i < n; i++) {
            left[i] = 1;
            right[i] = 1;
        }
     
        // Iterate over the array arr[] to
        // store length of decreasing
        // sequence that can be obtained
        // at every index in the right array
        for (int i = n - 2; i >= 0; i--) {
     
            if (a[i] > a[i + 1]) {
                right[i] = right[i + 1] + 1;
            }
     
            // Store the length of longest
            // continuous decreasing
            // sequence in maximum
            maximum = Math.max(maximum, right[i]);
        }
     
        // Iterate over the array arr[] to
        // store length of decreasing
        // sequence that can be obtained
        // at every index in the left array
        for (int i = 1; i < n; i++) {
            if (a[i] < a[i - 1]) {
                left[i] = left[i - 1] + 1;
            }
        }
     
        if (n > 2) {
            // Check if we can obtain a
            // longer decreasing sequence
            // after removal of any element
            // from the array arr[] with
            // the help of left[] & right[]
            for (int i = 1; i < n - 1; i++) {
                if (a[i - 1] > a[i + 1]) {
                    maximum = Math.max(maximum, left[i - 1] + right[i + 1]);
                }
            }
        }
     
        // Return maximum length of sequence
        return maximum;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = { 8, 7, 3, 5, 2, 9 };
        int n = arr.length;
     
        // Function calling
        System.out.println(maxLength(arr, n));
    }  
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to find maximum length
# of decreasing sequence by removing
# at most one element
 
# Function to find the maximum length
def maxLength(a, n) :
 
    # Intialise maximum length to 1
    maximum = 1;
 
    # Intialise left[] to find the
    # length of decreasing sequence
    # from left to right
    left = [0]*n;
 
    # Intialise right[] to find the
    # length of decreasing sequence
    # from right to left
    right = [0]*n;
 
    # Initially store 1 at each index of
    # left and right array
    for i in range(n) :
        left[i] = 1;
        right[i] = 1;
 
    # Iterate over the array arr[] to
    # store length of decreasing
    # sequence that can be obtained
    # at every index in the right array
    for i in range(n - 2, -1, -1) :
 
        if (a[i] > a[i + 1]) :
            right[i] = right[i + 1] + 1;
 
        # Store the length of longest
        # continuous decreasing
        # sequence in maximum
        maximum = max(maximum, right[i]);
 
    # Iterate over the array arr[] to
    # store length of decreasing
    # sequence that can be obtained
    # at every index in the left array
    for i in range(1, n) :
        if (a[i] < a[i - 1]) :
            left[i] = left[i - 1] + 1;
 
    if (n > 2) :
        # Check if we can obtain a
        # longer decreasing sequence
        # after removal of any element
        # from the array arr[] with
        # the help of left[] & right[]
        for i in range(1, n -1) :
            if (a[i - 1] > a[i + 1]) :
                maximum = max(maximum, left[i - 1] + right[i + 1]);
 
    # Return maximum length of sequence
    return maximum;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 8, 7, 3, 5, 2, 9 ];
    n = len(arr);
 
    # Function calling
    print(maxLength(arr, n));
 
# This code is contributed by AnkitRai01


C#
// C# program to find maximum length
// of decreasing sequence by removing
// at most one element
using System;
 
class GFG {
     
    // Function to find the maximum length
    static int maxLength(int []a, int n)
    {
        // Intialise maximum length to 1
        int maximum = 1;
     
        // Intialise left[] to find the
        // length of decreasing sequence
        // from left to right
        int []left = new int[n];
     
        // Intialise right[] to find the
        // length of decreasing sequence
        // from right to left
        int []right = new int[n];
     
        // Initially store 1 at each index of
        // left and right array
        for (int i = 0; i < n; i++) {
            left[i] = 1;
            right[i] = 1;
        }
     
        // Iterate over the array arr[] to
        // store length of decreasing
        // sequence that can be obtained
        // at every index in the right array
        for (int i = n - 2; i >= 0; i--) {
     
            if (a[i] > a[i + 1]) {
                right[i] = right[i + 1] + 1;
            }
     
            // Store the length of longest
            // continuous decreasing
            // sequence in maximum
            maximum = Math.Max(maximum, right[i]);
        }
     
        // Iterate over the array arr[] to
        // store length of decreasing
        // sequence that can be obtained
        // at every index in the left array
        for (int i = 1; i < n; i++) {
            if (a[i] < a[i - 1]) {
                left[i] = left[i - 1] + 1;
            }
        }
     
        if (n > 2) {
            // Check if we can obtain a
            // longer decreasing sequence
            // after removal of any element
            // from the array arr[] with
            // the help of left[] & right[]
            for (int i = 1; i < n - 1; i++) {
                if (a[i - 1] > a[i + 1]) {
                    maximum = Math.Max(maximum, left[i - 1] + right[i + 1]);
                }
            }
        }
     
        // Return maximum length of sequence
        return maximum;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int []arr = { 8, 7, 3, 5, 2, 9 };
        int n = arr.Length;
     
        // Function calling
        Console.WriteLine(maxLength(arr, n));
    }  
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
4

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live