Code Posted at ->

Working Java code for each of these puzzles available at http://datastructure-puzzles-codesolutions.blogspot.com


Code Downloads from github https://github.com/inders/datastructure-repo

Monday, April 19, 2010

Longest increasing subsequence in chain of numbers

Find out the the longest increasing subsequence in a sequence of 'n' numbers.
eg: 1, 5, 3, 2, 100

5 comments:

Aravind said...

Here x is first element and y is successive of x. Call this method as lcs(1,2,1); x,y are first,second element of array. len is initial length which is 1.



void lcs(int x, int y, int len) {
if( y <= array.length-1){
if( array[x] <= array[y]){
if( largest < len +1){
largest = len+1;
}
lcs(y,y+1,len+1);
}else{
lcs(x-1,y,len -1);
lcs(x,y+1,len);
}

}
}

Aravind said...

Recursive equation;

f(x,y) = {
x <= y , f(y,y+1,len+1)
x > y , f(x-1,y,len-1), f(x,y+1,len)
}

messiah said...

Use dynamic programing O(n) solution

harry said...

can u please write the code or give some link or @LEAST xplain ur logic of 0(n)

Paul said...

O(n) is impossible.
O(n log n) :
http://en.wikipedia.org/wiki/Patience_sorting