You are given an array of numbers, say
nums = [2, 5, 3, 3, 4, 6]
And want to get the longest possible sequence of numbers, that are ascending, though not necessarily consequent, while maintaining their order.
So the longest array of numbers where An < An+1. In this case:
[2, 3, 4, 6]
I have done this via recursion and loops, testing every possibility. This however takes way too much time for larger arrays and so my question is, whether there is a better/faster method to do this.
Thanks in advance!
Here's my previous code, which returned the length of the final array
def bestLine(arr):
maximum = 0
for i in range(0, len(arr)):
if (len(arr)-i < maximum):
break
maximum = max(maximum, f(i, len(arr), arr))
return maximum
def f(start, end, arr):
best = 0
for i in range(start+1, end):
if (end-i < best):
break
if (arr[i] > arr[start]):
best = max(best, f(i, end, arr))
return 1 + best