📜  数组边界元素的最长递增序列

📅  最后修改于: 2021-09-06 06:26:23             🧑  作者: Mango

给定一个长度为N且具有唯一元素的数组arr[] ,任务是找到可以由数组两端的元素组成的最长递增子序列的长度。
例子:

方法:这个问题可以通过两个指针方法来解决。在数组的第一个和最后一个索引处设置两个指针。选择当前指向的两个值中的最小值并检查它是否大于先前选择的值。如果是,则更新指针并增加 LIS 的长度并重复该过程。否则,打印获得的 LIS 的长度。
下面是上述方法的实现:

C++
// C++ Program to print the
// longest increasing
// subsequence from the
// boundary elements of an array
 
#include 
using namespace std;
 
// Function to return the length of
// Longest Increasing subsequence
int longestSequence(int n, int arr[])
{
 
    // Set pointers at
    // both ends
    int l = 0, r = n - 1;
 
    // Stores the recent
    // value added to the
    // subsequence
    int prev = INT_MIN;
 
    // Stores the length of
    // the subsequence
    int ans = 0;
 
    while (l <= r) {
 
        // Check if both elements
        // can be added to the
        // subsequence
        if (arr[l] > prev
            && arr[r] > prev) {
 
            if (arr[l] < arr[r]) {
                ans += 1;
                prev = arr[l];
                l += 1;
            }
            else {
                ans += 1;
                prev = arr[r];
                r -= 1;
            }
        }
 
        // Check if the element
        // on the left can be
        // added to the
        // subsequence only
        else if (arr[l] > prev) {
            ans += 1;
            prev = arr[l];
            l += 1;
        }
 
        // Check if the element
        // on the right can be
        // added to the
        // subsequence only
        else if (arr[r] > prev) {
            ans += 1;
            prev = arr[r];
            r -= 1;
        }
 
        // If none of the values
        // can be added to the
        // subsequence
        else {
            break;
        }
    }
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 5, 1, 4, 2 };
 
    // Length of array
    int n = sizeof(arr)
            / sizeof(arr[0]);
 
    cout << longestSequence(n, arr);
 
    return 0;
}


Java
// Java program to print the longest
// increasing subsequence from the
// boundary elements of an array
import java.util.*;
 
class GFG{
 
// Function to return the length of
// Longest Increasing subsequence
static int longestSequence(int n, int arr[])
{
     
    // Set pointers at
    // both ends
    int l = 0, r = n - 1;
 
    // Stores the recent
    // value added to the
    // subsequence
    int prev = Integer.MIN_VALUE;
 
    // Stores the length of
    // the subsequence
    int ans = 0;
 
    while (l <= r)
    {
         
        // Check if both elements
        // can be added to the
        // subsequence
        if (arr[l] > prev &&
            arr[r] > prev)
        {
            if (arr[l] < arr[r])
            {
                ans += 1;
                prev = arr[l];
                l += 1;
            }
            else
            {
                ans += 1;
                prev = arr[r];
                r -= 1;
            }
        }
 
        // Check if the element on the
        // left can be added to the
        // subsequence only
        else if (arr[l] > prev)
        {
            ans += 1;
            prev = arr[l];
            l += 1;
        }
 
        // Check if the element on the
        // right can be added to the
        // subsequence only
        else if (arr[r] > prev)
        {
            ans += 1;
            prev = arr[r];
            r -= 1;
        }
 
        // If none of the values
        // can be added to the
        // subsequence
        else
        {
            break;
        }
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 5, 1, 4, 2 };
 
    // Length of array
    int n = arr.length;
 
    System.out.print(longestSequence(n, arr));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to print the
# longest increasing subsequence
# from the boundary elements
# of an array
import sys
 
# Function to return the length of
# Longest Increasing subsequence
def longestSequence(n, arr):
     
    # Set pointers at
    # both ends
    l = 0
    r = n - 1
     
    # Stores the recent value
    # added to the subsequence
    prev = -sys.maxsize - 1
     
    # Stores the length of
    # the subsequence
    ans = 0
     
    while (l <= r):
         
        # Check if both elements can be
        # added to the subsequence
        if (arr[l] > prev and
            arr[r] > prev):
         
            if (arr[l] < arr[r]):
                ans += 1
                prev = arr[l]
                l += 1
                 
            else:
                ans += 1
                prev = arr[r]
                r -= 1
                 
        # Check if the element
        # on the left can be
        # added to the
        # subsequence only
        elif (arr[l] > prev):
            ans += 1
            prev = arr[l]
            l += 1
         
        # Check if the element
        # on the right can be
        # added to the
        # subsequence only
        elif (arr[r] > prev):
            ans += 1
            prev = arr[r]
            r -= 1
         
        # If none of the values
        # can be added to the
        # subsequence
        else:
            break
         
    return ans
             
# Driver code
arr = [ 3, 5, 1, 4, 2 ]
 
# Length of array
n = len(arr)
 
print(longestSequence(n, arr))
 
# This code is contributed by sanjoy_62


C#
// C# program to print the longest
// increasing subsequence from the
// boundary elements of an array
using System;
 
class GFG{
 
// Function to return the length of
// longest Increasing subsequence
static int longestSequence(int n, int []arr)
{
     
    // Set pointers at
    // both ends
    int l = 0, r = n - 1;
 
    // Stores the recent value
    // added to the subsequence
    int prev = int.MinValue;
 
    // Stores the length of
    // the subsequence
    int ans = 0;
 
    while (l <= r)
    {
         
        // Check if both elements
        // can be added to the
        // subsequence
        if (arr[l] > prev &&
            arr[r] > prev)
        {
            if (arr[l] < arr[r])
            {
                ans += 1;
                prev = arr[l];
                l += 1;
            }
            else
            {
                ans += 1;
                prev = arr[r];
                r -= 1;
            }
        }
 
        // Check if the element on the
        // left can be added to the
        // subsequence only
        else if (arr[l] > prev)
        {
            ans += 1;
            prev = arr[l];
            l += 1;
        }
 
        // Check if the element on the
        // right can be added to the
        // subsequence only
        else if (arr[r] > prev)
        {
            ans += 1;
            prev = arr[r];
            r -= 1;
        }
 
        // If none of the values
        // can be added to the
        // subsequence
        else
        {
            break;
        }
    }
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 3, 5, 1, 4, 2 };
 
    // Length of array
    int n = arr.Length;
 
    Console.Write(longestSequence(n, arr));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:

4

时间复杂度: O(N)

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