You are given an array of numbers, say
nums = [2, 5, 3, 3, 4, 6]
And I want to get the maximum possible sequence of numbers that increase, although they do not necessarily follow, while maintaining their order.
So the longest array of numbers, where A n<A <sub> n + 1sub>. In this case:
[2, 3, 4, 6]
I did this with recursion and loops, checking every possibility. This, however, takes too much time for large arrays and so my question is whether there is a better / faster way for this.
Thanks in advance!
Here is my previous code that 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
source
share