User:GeorgeBills/Strictly increasing sequence

The original page is at longest increasing subsequence problem.


Longest common sequence version

edit

Simple version

edit

The following is code for finding the longest strictly increasing sequence [1]:

int lsis( int* a, int N ) {
   int *best, *prev, i, j, max = 0;
   best = (int*) malloc ( sizeof( int ) * N );
   prev = (int*) malloc ( sizeof( int ) * N );

   for ( i = 0; i < N; i++ ) best[i] = 1, prev[i] = i;

   for ( i = 1; i < N; i++ )
      for ( j = 0; j < i; j++ )
         if ( a[i] > a[j] && best[i] < best[j] + 1 )
            best[i] = best[j] + 1, prev[i] = j; 

   for ( i = 0; i < N; i++ )
      if ( max < best[i] )
         max = best[i];

   free( best );
   free( prev );

   return max;
}

Optimized version

edit

See also

edit
edit

References

edit