← DiffPush

Longest Bitonic Subsequence

Standard Bar Dynamic Programming · DP on LIS O(n^2) · O(n)

The Cable-Car Ridge Profile

A mountain railway records the elevations of n pylons. A scenic route must climb strictly over a stretch of pylons and then descend strictly over the rest — a mountain shape. The survey team wants the longest such ride measured in pylons visited; a pure climb or a pure descent also counts as scenic.

Input: An array nums of n positive integers (pylon elevations).

Output: Return the length of the longest subsequence that is strictly increasing then strictly decreasing (either flank may be empty).

Constraints

Examples

Example 1

Input: {"nums":[1,2,1,5,4,2,6,4]}
Output: 5
1, 2, 5, 4, 2 — climb to 5 then fall to 2.

Example 2

Input: {"nums":[1,2,3,4,5]}
Output: 5
A pure climb qualifies with an empty descent flank.

Solve this in your browser →

Also on LeetCode ↗