📜  最长递增奇偶子序列

📅  最后修改于: 2021-04-29 17:02:37             🧑  作者: Mango

给定大小为n的数组。问题是找到给定数组中子序列的长度,以使子序列的所有元素都按升序排序,并且它们也交替为奇数和偶数。
注意,子序列可以以奇数或偶数开头。
例子:

Input : arr[] = {5, 6, 9, 4, 7, 8}
Output : 6
{5, 6, 9,4,7,8} is the required longest
increasing odd even subsequence which is the array itself in this case

Input : arr[] = {1, 12, 2, 22, 5, 30, 31, 14, 17, 11}
{1,12,5,30,31,14,17} is the required longest
increasing odd even subsequence
Output : 7

天真的方法:考虑所有子序列,并按升序选择具有交替的奇偶数的子序列。从其中选择最长的一个。这具有指数时间复杂度。
高效方法:
令L(i)为以索引i结尾的LIOES(最长递增奇数偶数子序列)的长度,以使arr [i]是LIOES的最后一个元素。
然后,L(i)可以递归写为:
L(i)= 1 + max(L(j))其中0 如果不存在这样的j,则L(i)= 1。
为了找到给定数组的LIOES,我们需要返回max(L(i)),其中0 对于上述递归关系,下面已经实现了一种动态编程方法。

C++
// C++ implementation to find the longest
// increasing odd even subsequence
#include 
 
using namespace std;
 
// function to find the longest
// increasing odd even subsequence
int longOddEvenIncSeq(int arr[], int n)
{
    // lioes[i] stores longest increasing odd
    // even subsequence ending at arr[i]
    int lioes[n];
 
    // to store the length of longest increasing
    // odd even subsequence
    int maxLen = 0;
 
    // Initialize LIOES values for all indexes
    for (int i = 0; i < n; i++)
        lioes[i] = 1;
 
    // Compute optimized LIOES values
    // in bottom up manner
    for (int i = 1; i < n; i++)
        for (int j = 0; j < i; j++)
            if (arr[i] > arr[j] &&
               (arr[i] + arr[j]) % 2 != 0
                && lioes[i] < lioes[j] + 1)
                lioes[i] = lioes[j] + 1;
 
    // Pick maximum of all LIOES values
    for (int i = 0; i < n; i++)
        if (maxLen < lioes[i])
            maxLen = lioes[i];
 
    // required maximum length
    return maxLen;
}
 
// Driver program to test above
int main()
{
    int arr[] = { 1, 12, 2, 22, 5, 30,
                    31, 14, 17, 11 };
    int n = sizeof(arr) / sizeof(n);
    cout << "Longest Increasing Odd Even "
         << "Subsequence: "
         << longOddEvenIncSeq(arr, n);
    return 0;
}


Java
// Java implementation to find the longest
// increasing odd even subsequence
import java.util.*;
import java.lang.*;
 
public class GfG{
     
    // function to find the longest
    // increasing odd even subsequence
    public static int longOddEvenIncSeq(int arr[],
                                           int n)
    {
        // lioes[i] stores longest increasing odd
        // even subsequence ending at arr[i]
        int[] lioes = new int[n];
 
        // to store the length of longest
        // increasing odd even subsequence
        int maxLen = 0;
 
        // Initialize LIOES values for all indexes
        for (int i = 0; i < n; i++)
            lioes[i] = 1;
 
        // Compute optimized LIOES values
        // in bottom up manner
        for (int i = 1; i < n; i++)
            for (int j = 0; j < i; j++)
                if (arr[i] > arr[j] &&
                (arr[i] + arr[j]) % 2 != 0
                    && lioes[i] < lioes[j] + 1)
                    lioes[i] = lioes[j] + 1;
 
        // Pick maximum of all LIOES values
        for (int i = 0; i < n; i++)
            if (maxLen < lioes[i])
                maxLen = lioes[i];
 
        // required maximum length
        return maxLen;
    }
    // driver function
    public static void main(String argc[]){
        int[] arr = new int[]{ 1, 12, 2, 22,
                     5, 30, 31, 14, 17, 11 };
        int n = 10;
        System.out.println("Longest Increasing Odd"
                          + " Even Subsequence: "
                       + longOddEvenIncSeq(arr, n));
    }
}
 
/* This code is contributed by Sagar Shukla */


Python3
# Python3 implementation to find the longest
# increasing odd even subsequence
 
# function to find the longest
# increasing odd even subsequence
def longOddEvenIncSeq( arr , n ):
 
    # lioes[i] stores longest increasing odd
    # even subsequence ending at arr[i]
    lioes = list()
     
    # to store the length of longest increasing
    # odd even subsequence
    maxLen = 0
     
    # Initialize LIOES values for all indexes
    for i in range(n):
        lioes.append(1)
         
    # Compute optimized LIOES values
    # in bottom up manner
    i=1
    for i in range(n):
        for j in range(i):
            if (arr[i] > arr[j] and
                (arr[i] + arr[j]) % 2 != 0 and
                lioes[i] < lioes[j] + 1):
                    lioes[i] = lioes[j] + 1
     
    # Pick maximum of all LIOES values
    for i in range(n):
        if maxLen < lioes[i]:
            maxLen = lioes[i]
             
    # required maximum length
    return maxLen
     
# Driver to test above
arr = [ 1, 12, 2, 22, 5, 30, 31, 14, 17, 11 ]
n = len(arr)
print("Longest Increasing Odd Even " +
      "Subsequence: ",longOddEvenIncSeq(arr, n))
                 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# implementation to find the longest
// increasing odd even subsequence
using System;
 
class GFG {
 
    // function to find the longest
    // increasing odd even subsequence
    public static int longOddEvenIncSeq(int[] arr,
                                            int n)
    {
        // lioes[i] stores longest increasing odd
        // even subsequence ending at arr[i]
        int[] lioes = new int[n];
 
        // to store the length of longest
        // increasing odd even subsequence
        int maxLen = 0;
 
        // Initialize LIOES values for all indexes
        for (int i = 0; i < n; i++)
            lioes[i] = 1;
 
        // Compute optimized LIOES values
        // in bottom up manner
        for (int i = 1; i < n; i++)
            for (int j = 0; j < i; j++)
                if (arr[i] > arr[j] &&
                   (arr[i] + arr[j]) % 2 != 0 &&
                    lioes[i] < lioes[j] + 1)
                     
                    lioes[i] = lioes[j] + 1;
 
        // Pick maximum of all LIOES values
        for (int i = 0; i < n; i++)
            if (maxLen < lioes[i])
                maxLen = lioes[i];
 
        // required maximum length
        return maxLen;
    }
     
    // driver function
    public static void Main()
    {
        int[] arr = new int[]{ 1, 12, 2, 22,
                               5, 30, 31, 14, 17, 11 };
        int n = 10;
        Console.Write("Longest Increasing Odd"
                    + " Even Subsequence: "
                    + longOddEvenIncSeq(arr, n));
    }
}
 
// This code is contributed by Sam007


PHP
 $arr[$j] &&
            ($arr[$i] + $arr[$j]) % 2 != 0 &&
                $lioes[$i] < $lioes[$j] + 1)
                $lioes[$i] = $lioes[$j] + 1;
 
    // Pick maximum of all LIOES values
    for ($i = 0; $i < $n; $i++)
        if ($maxLen < $lioes[$i])
            $maxLen = $lioes[$i];
 
    // required maximum length
    return $maxLen;
}
 
// Driver Code
$arr = array( 1, 12, 2, 22, 5, 30,
                    31, 14, 17, 11) ;
$n = sizeof($arr);
echo "Longest Increasing Odd Even ".
        "Subsequence: " . longOddEvenIncSeq($arr, $n);
 
// This code is contributed
// by ChitraNayal
?>


Javascript


输出:

Longest Increasing Odd Even Subsequence: 5

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