[GeeksForGeeks] Longest Bitonic Subsequence

Given an array arr[0.....n-1] containing n positive integers, find the length of the longest bitonic subsequence.

A subsequence of arr[] is called Bitonic if it is first increasing, then decreasing.

Analysis:

This problem is a variation of the standard Longest Increasing Subsequence(LIS) problem.  In the standard LIS

problem, we use dynamic programming and create a 1D array lis[i], lis[i] is the length of the longest increasing subsequence

that ends with arr[i].

In this problem, we construct two arrays lis[i] and ids[i] using dynamic programming.

lis[i] stores the length of the longest increasing subsequence ending with arr[i];

lds[i] stores the length of the longest decreasing subsequence starting from arr[i];

Finally, we need to return the max value of lis[i] + lds[i] - 1, where i is from 0 to n - 1.

 1 public class Solution {
 2     public int longestBitonicSubsequence(int[] arr){
 3         if(arr == null || arr.length == 0){
 4             return 0;
 5         }
 6         int[] lis = new int[arr.length];
 7         int[] lds = new int[arr.length];
 8         for(int i = 0; i < arr.length; i++){
 9             lis[i] = 1;
10             lds[i] = 1;
11         }
12         for(int i = 0; i < arr.length; i++){
13             for(int j = 0; j < i; j++){
14                 if(arr[i] > arr[j] && lis[i] < lis[j] + 1){
15                     lis[i] = lis[j] + 1;
16                 }
17             }
18         }
19         for(int i = arr.length - 1; i >= 0; i--){
20             for(int j = arr.length - 1; j > i; j--){
21                 if(arr[i] > arr[j] && lds[i] < lds[j] + 1){
22                     lds[i] = lds[j] + 1;
23                 }
24             }
25         }
26         int max = 0;
27         for(int i = 0; i < arr.length; i++){
28             if(lis[i] + lds[i] - 1 > max){
29                 max = lis[i] + lds[i] - 1;
30             }
31         }
32         return max;
33     }
34 }

Related Problems

Longest Increasing Subsequence

时间: 2024-10-09 16:48:33

[GeeksForGeeks] Longest Bitonic Subsequence的相关文章

[Coding Made Simple] Longest Bitonic Subsequence

Find longest bitonic subsequence in given array. Bitonic subsequence first increases then decreases. Same problem link Longest Bitonic Subsequence

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

中南OJ1551: Longest Increasing Subsequence Again(分块+离散化线段树)

1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 29  Solved: 15 [Submit][Status][Web Board] Description Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the

300. Longest Increasing Subsequence

Problem statement: Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there

poj 2533 Longest Ordered Subsequence(LIS)

Description A numeric sequence of ai is ordered ifa1 <a2 < ... < aN. Let the subsequence of the given numeric sequence (a1,a2, ..., aN) be any sequence (ai1,ai2, ..., aiK), where 1 <=i1 < i2 < ... < iK <=N. For example, sequence (1

[2016-04-01][poj][2533][Longest Ordered Subsequence]

时间:2016-04-01 21:35:02 星期五 题目编号:[2016-04-01][poj][2533][Longest Ordered Subsequence] #include <cstdio> #include <algorithm> using namespace std; int dp[1000 + 10],a[1000 + 10]; int main(){ int n; while(~scanf("%d",&n)){ for(int i

SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19929 Description Given a sequence of N pairs of integers, find the length of the longest incre

Dynamic Programming | Set 3 (Longest Increasing Subsequence)

在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Optimal Substructure Property) 中我们已经讨论了重叠子问题和最优子结构性质,现在我们来看一个可以使用动态规划来解决的问题:最长上升子序列(Longest Increasing Subsequence(LIS)). 最长上升子序列问题,致力于在一个给定的序列中找到一个最长的子序列