tags:

views:

252

answers:

4

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
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
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
it is unsorted. so for exampleif you have 0 2 4 5 3 6 1 9 8you will get 0 2 4 5
SuperString
A: 

As Mehrdad suggests, LIS is at least close to what you need. This is most efficiently implemented using dynamic programming / memoization. If you are interested in stuff like this, I recommend you head over to TopCoder

John
+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&amp;d1=tutorials&amp;d2=dynProg

chethu