给定一个大小为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) 是 LIOES(最长递增奇偶子序列)的长度,以索引 i 结束,使得 arr[i] 是 LIOES 的最后一个元素。
然后,L(i) 可以递归地写为:
L(i) = 1 + max( L(j) ) 其中 0 < j < i and (arr[j] < arr[i]) and (arr[i]+arr[j])%2 != 0;要么
L(i) = 1,如果不存在这样的 j。
要找到给定数组的 LIOES,我们需要返回 max(L(i)) 其中 0 < i < n。
下面针对上述递归关系实现了动态规划方法。
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)。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。