Longest Bitonic Subsequence
InterviewTime limit1sMemory limit256 MB
Given a sequence of up to 1000 numbers, find the length of its longest subsequence that strictly rises then strictly falls.
- Level
Medium5 of 10
- Topics
- Dynamic programming
- Solved
- No attempts yet
Problem
A sequence is bitonic if it has an element such that . Either the increasing part or the decreasing part may be empty, so a strictly increasing sequence and a strictly decreasing sequence are both bitonic.
For example, {10, 20, 30, 25, 20}, {10, 20, 30, 40}, {50, 40, 25, 10} are bitonic sequences, while {1, 2, 3, 2, 1, 2, 3, 2, 1} and {10, 20, 30, 40, 20, 30} are not.
Given a sequence , find the length of the longest subsequence of that is bitonic. A subsequence is what is left after deleting zero or more elements of and keeping the order of the remaining elements.
Input
The first line has the size of the sequence . The second line has , separated by spaces. (, )
Output
Print on the first line the length of the longest bitonic subsequence of .
Hint
In the first example, 1, 2, 3, 4, 5, 2, 1 is a longest bitonic subsequence, and its length is 7.