Search⌘ K
AI Features

Longest Bitonic Subsequence

Explore the problem of finding the longest bitonic subsequence in an array of positive integers. Understand both naive recursive and optimized dynamic programming solutions, including top-down memoization and bottom-up tabulation approaches. This lesson helps you implement efficient algorithms, analyze time and space complexities, and prepares you for related coding interview questions.

Statement

Suppose you are given an array, nums, containing positive integers. You need to find the length of the longest bitonic subsequence in this array. A bitonic subsequence can be of the following three types:

  • It can consist of numbers that are first increasing and then decreasing. For example, (2,6,9,3,2)(2, 6, 9, 3, 2).

  • It can consist of numbers that are only increasing (where the decreasing part at the end is empty). For example, (2,3,7,9)(2, 3, 7, 9).

  • It can consist of numbers that are only decreasing (where the increasing part at the start is empty). For example, (15,12,5,3,2,1)(15, 12, 5, 3, 2, 1).

Let’s say you have the following array:

  • [19,20,5,3,13][19, 20, 5, 3, 13]

The longest bitonic subsequence from this array is (19,20,5,3)(19, 20, 5, 3), and the length of this subsequence is 44.

Constraints:

  • 11\leq nums.length 3×103\leq 3 \times 10^3
  • 11\leq nums[i] 104\leq 10^4
...