📜  打印最长双调子序列(空间优化方法)

📅  最后修改于: 2021-09-08 12:26:30             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是打印给定数组的最长双调子序列。
注意:如果存在多个解决方案,则打印任何解决方案。

例子:

Dynamic Programming Approach using Extra Space:参考上一篇文章,解决问题的二维动态规划方法。
时间复杂度: O(N 2 )
辅助空间: O(N 2 )

空间优化方法:用于上述方法的辅助空间可以使用一维动态规划进行优化。请按照以下步骤解决问题。

  1. 创建两个数组lis[]lds[]以在每个i索引处存储分别以元素arr[i]结尾的最长递增和递减子序列的长度。
  2. 计算后,找到包含lis[i] + lds[i] – 1最大值的i索引
  3. 创建res[]以按元素降序存储最长双音序列的所有元素,然后是元素升序。
  4. 打印res[]数组。

以下是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print the longest
// bitonic subsequence
void printRes(vector& res)
{
    int n = res.size();
    for (int i = 0; i < n; i++) {
        cout << res[i] << " ";
    }
}
 
// Function to generate the longest
// bitonic subsequence
void printLBS(int arr[], int N)
{
 
    // Store the lengths of LIS
    // ending at every index
    int lis[N];
 
    // Store the lengths of LDS
    // ending at every index
    int lds[N];
 
    for (int i = 0; i < N; i++) {
        lis[i] = lds[i] = 1;
    }
 
    // Compute LIS for all indices
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < i; j++) {
 
            if (arr[j] < arr[i]) {
 
                if (lis[i] < lis[j] + 1)
                    lis[i] = lis[j] + 1;
            }
        }
    }
 
    // Compute LDS for all indices
    for (int i = N - 1; i >= 0; i--) {
 
        for (int j = N - 1; j > i; j--) {
            if (arr[j] < arr[i]) {
 
                if (lds[i] < lds[j] + 1)
                    lds[i] = lds[j] + 1;
            }
        }
    }
 
    // Find the index having
    // maximum value of
    // lis[i] + lds[i] - 1
    int MaxVal = arr[0], inx = 0;
    for (int i = 0; i < N; i++) {
 
        if (MaxVal < lis[i] + lds[i] - 1) {
            MaxVal = lis[i] + lds[i] - 1;
            inx = i;
        }
    }
 
    // Stores the count of elements in
    // increasing order in Bitonic subsequence
    int ct1 = lis[inx];
    vector res;
 
    // Store the increasing subsequence
    for (int i = inx; i >= 0 && ct1 > 0; i--) {
 
        if (lis[i] == ct1) {
 
            res.push_back(arr[i]);
 
            ct1--;
        }
    }
 
    // Sort the bitonic subsequence
    // to arrange smaller elements
    // at the beginning
    reverse(res.begin(), res.end());
 
    // Stores the count of elements in
    // decreasing order in Bitonic subsequence
    int ct2 = lds[inx] - 1;
    for (int i = inx; i < N && ct2 > 0; i++) {
 
        if (lds[i] == ct2) {
 
            res.push_back(arr[i]);
 
            ct2--;
        }
    }
 
    // Print the longest
    // bitonic sequence
    printRes(res);
}
 
// Driver Code
int main()
{
 
    int arr[] = { 80, 60, 30, 40, 20, 10 };
    int N = sizeof(arr) / sizeof(arr[0]);
    printLBS(arr, N);
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG {
 
// Function to print the longest
// bitonic subsequence
static void printRes(Vector res)
{
    Enumeration enu = res.elements();
    while (enu.hasMoreElements())
    {
        System.out.print(enu.nextElement() + " ");
    }
}
 
// Function to generate the longest
// bitonic subsequence
static void printLBS(int arr[], int N)
{
 
    // Store the lengths of LIS
    // ending at every index
    int lis[] = new int[N];
 
    // Store the lengths of LDS
    // ending at every index
    int lds[] = new int[N];
 
    for(int i = 0; i < N; i++)
    {
        lis[i] = lds[i] = 1;
    }
 
    // Compute LIS for all indices
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < i; j++)
        {
            if (arr[j] < arr[i])
            {
                if (lis[i] < lis[j] + 1)
                    lis[i] = lis[j] + 1;
            }
        }
    }
 
    // Compute LDS for all indices
    for(int i = N - 1; i >= 0; i--)
    {
        for(int j = N - 1; j > i; j--)
        {
            if (arr[j] < arr[i])
            {
                if (lds[i] < lds[j] + 1)
                    lds[i] = lds[j] + 1;
            }
        }
    }
 
    // Find the index having
    // maximum value of
    // lis[i] + lds[i] - 1
    int MaxVal = arr[0], inx = 0;
    for(int i = 0; i < N; i++)
    {
        if (MaxVal < lis[i] + lds[i] - 1)
        {
            MaxVal = lis[i] + lds[i] - 1;
            inx = i;
        }
    }
 
    // Stores the count of elements in
    // increasing order in Bitonic subsequence
    int ct1 = lis[inx];
    Vector res = new Vector();
 
    // Store the increasing subsequence
    for(int i = inx; i >= 0 && ct1 > 0; i--)
    {
        if (lis[i] == ct1)
        {
            res.add(arr[i]);
 
            ct1--;
        }
    }
 
    // Sort the bitonic subsequence
    // to arrange smaller elements
    // at the beginning
    Collections.reverse(res);
     
    // Stores the count of elements in
    // decreasing order in Bitonic subsequence
    int ct2 = lds[inx] - 1;
    for(int i = inx; i < N && ct2 > 0; i++)
    {
        if (lds[i] == ct2)
        {
            res.add(arr[i]);
            ct2--;
        }
    }
 
    // Print the longest
    // bitonic sequence
    printRes(res);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 80, 60, 30, 40, 20, 10 };
    int N = arr.length;
     
    printLBS(arr, N);
}
}
 
// This code is contributed by chitranayal


Python3
# Python3 program to implement
# the above approach
 
# Function to print the longest
# bitonic subsequence
def printRes(res):
     
    n = len(res)
    for i in range(n):
        print(res[i], end = " ")
         
# Function to generate the longest
# bitonic subsequence
def printLBS(arr, N):
     
    # Store the lengths of LIS
    # ending at every index
    lis = [0] * N
     
    # Store the lengths of LDS
    # ending at every index
    lds = [0] * N
     
    for i in range(N):
        lis[i] = lds[i] = 1
         
    # Compute LIS for all indices
    for i in range(N):
        for j in range(i):
            if arr[j] < arr[i]:
                if lis[i] < lis[j] + 1:
                    lis[i] = lis[j] + 1
                 
    # Compute LDS for all indices
    for i in range(N - 1, -1, -1):
        for j in range(N - 1, i, -1):
            if arr[j] < arr[i]:
                if lds[i] < lds[j] + 1:
                    lds[i] = lds[j] + 1
                     
    # Find the index having
    # maximum value of
    # lis[i]+lds[i]+1
    MaxVal = arr[0]
    inx = 0
     
    for i in range(N):
        if MaxVal < lis[i] + lds[i] - 1:
            MaxVal = lis[i] + lds[i] - 1
            inx = i
             
    # Stores the count of elements in
    # increasing order in Bitonic subsequence
    ct1 = lis[inx]
    res = []
     
    i = inx
     
    # Store the increasing subsequence
    while i >= 0 and ct1 > 0:
        if lis[i] == ct1:
            res.append(arr[i])
            ct1 -= 1
             
        i -= 1
         
    # Sort the bitonic subsequence
    # to arrange smaller elements
    # at the beginning
    res.reverse()
     
    # Stores the count of elements in
    # decreasing order in Bitonic subsequence
    ct2 = lds[inx] - 1
    i = inx
     
    while i < N and ct2 > 0:
        if lds[i] == ct2:
            res.append(arr[i])
            ct2 -= 1
             
        i += 1
     
    # Print the longest
    # bitonic sequence
    printRes(res)
     
# Driver code
arr = [ 80, 60, 30, 40, 20, 10 ]
N = len(arr)
 
printLBS(arr, N)
 
# This code is contributed by Stuti Pathak


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
    // Function to print the longest
    // bitonic subsequence
    static void printRes(List res)
    {
        foreach(int enu in res)
        {
            Console.Write(enu + " ");
        }
    }
 
    // Function to generate the longest
    // bitonic subsequence
    static void printLBS(int[] arr, int N)
    {
 
        // Store the lengths of LIS
        // ending at every index
        int[] lis = new int[N];
 
        // Store the lengths of LDS
        // ending at every index
        int[] lds = new int[N];
 
        for (int i = 0; i < N; i++)
        {
            lis[i] = lds[i] = 1;
        }
 
        // Compute LIS for all indices
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (arr[j] < arr[i])
                {
                    if (lis[i] < lis[j] + 1)
                        lis[i] = lis[j] + 1;
                }
            }
        }
 
        // Compute LDS for all indices
        for (int i = N - 1; i >= 0; i--)
        {
            for (int j = N - 1; j > i; j--)
            {
                if (arr[j] < arr[i])
                {
                    if (lds[i] < lds[j] + 1)
                        lds[i] = lds[j] + 1;
                }
            }
        }
 
        // Find the index having
        // maximum value of
        // lis[i] + lds[i] - 1
        int MaxVal = arr[0], inx = 0;
        for (int i = 0; i < N; i++)
        {
            if (MaxVal < lis[i] + lds[i] - 1)
            {
                MaxVal = lis[i] + lds[i] - 1;
                inx = i;
            }
        }
 
        // Stores the count of elements in
        // increasing order in Bitonic subsequence
        int ct1 = lis[inx];
        List res = new List();
 
        // Store the increasing subsequence
        for (int i = inx; i >= 0 && ct1 > 0; i--)
        {
            if (lis[i] == ct1)
            {
                res.Add(arr[i]);
                ct1--;
            }
        }
 
        // Sort the bitonic subsequence
        // to arrange smaller elements
        // at the beginning
        res.Reverse();
 
        // Stores the count of elements in
        // decreasing order in Bitonic subsequence
        int ct2 = lds[inx] - 1;
        for (int i = inx; i < N && ct2 > 0; i++)
        {
            if (lds[i] == ct2)
            {
                res.Add(arr[i]);
                ct2--;
            }
        }
 
        // Print the longest
        // bitonic sequence
        printRes(res);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int[] arr = {80, 60, 30, 40, 20, 10};
        int N = arr.Length;
        printLBS(arr, N);
    }
}
 
// This code is contributed by Amit Katiyar


输出:
80 60 30 20 10


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

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