leftLIS
and rightLIS
of the same length as the input array nums
to store the length of the LIS ending and starting at each element, respectively.leftLIS
array by computing the LIS for each element from left to right.rightLIS
array by computing the LIS for each element from right to left.maxLen
to keep track of the maximum length of the bitonic subsequence found.leftLIS[i]
and rightLIS[i]
are greater than or equal to 2, calculate the length of the bitonic subsequence as leftLIS[i] + rightLIS[i] - 1
and update maxLen
if it's greater.maxLen
, which represents the minimum number of elements to remove.