educative.io

Educative

Alternative solution for longest bitonic sequence

Hi @Design_Gurus
Can this be an alternative solution to the longest bitonic sequence?
Thanks

#Longest Bitonic Subsequence - Python alternative solution
def find_LBS(nums):
  def find_LBS_recursive(nums, prevIndex, currIndex, flag):
    if currIndex == len(nums):
      return 0
    c1 = 0
    if prevIndex == -1 or (not flag and nums[prevIndex] < nums[currIndex] ):
      c1 = 1 + find_LBS_recursive(nums, currIndex, currIndex + 1,flag)
    # Below two cases can be combined to form a single case
    # elif (not flag and nums[prevIndex] > nums[currIndex] ):
    #   c1 = 1 + find_LBS_recursive(nums, currIndex, currIndex + 1, 1)
    # elif (flag and nums[prevIndex] > nums[currIndex] ):
    #   c1 = 1 + find_LBS_recursive(nums, currIndex, currIndex + 1, flag)
    elif ( nums[prevIndex] > nums[currIndex] ):
      c1 = 1 + find_LBS_recursive(nums, currIndex, currIndex + 1, nums[prevIndex] > nums[currIndex])
    # Below case can be removed as c1=0 by default
    # elif (flag and nums[prevIndex] < nums[currIndex] ):
    #   c1 = 0
    c2 = find_LBS_recursive(nums, prevIndex, currIndex + 1, flag)
    return max(c1, c2)
  return find_LBS_recursive(nums, -1, 0, False)

It’s not working on my end. Can you send the executable version?