What's a fast algorithm for finding the length of largest monotonically increasing sequence in an array of integers.
+2
A:
From Wikipedia: Longest increasing subsequence (O(n log n))
L = 0
for i = 1, 2, ... n:
binary search for the largest positive j ≤ L such that X[M[j]] < X[i] (or set j = 0 if no such value exists)
P[i] = M[j]
if j == L or X[i] < X[M[j+1]]:
M[j+1] = i
L = max(L, j+1)
Mehrdad Afshari
2010-02-22 10:00:37
A:
Not sure what you mean by algorithm but here's an idea.
If the array is sorted by default (i.e when creating array laregst value is at the end due to it being an increasing sequence) then retrieve the last array value.
If not then create a new variable and loop through the array assigning the current value in loop if it is larger than the one stored in the temporary variable.
LnDCobra
2010-02-22 10:06:34
Regarding the answer above, making L=0 would not work if the array consists of only negative integers (as 0 is > -1). Therefore might want to set L to the first value in the array if it exists.
LnDCobra
2010-02-22 10:12:28
it is unsorted. so for exampleif you have 0 2 4 5 3 6 1 9 8you will get 0 2 4 5
SuperString
2010-02-22 10:16:17
+1
A:
you can use dynamic programming to solve this problem.The solution for this problem using dynamic programming is here:
http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg
chethu
2010-02-22 21:30:47