longest increasing subsequence

problem is like this:

Input is an int array: ->  10, 85, 96, 80, 93, 54, 3, 26, 4, 37

Output is an integer, in this example, you should return 3 (because the longest length is 3, i.e: 3, 26, 37, of course, there can be other subsequences with this length.)

How to solve this? 

Use a ArrayList! Always update the first number that is larger than the new comer. The procedure is like this:

->10

->10 ->85

->10 ->85 ->96

->10 ->80 ->96 (replace 85 with new comer 80)

->10 ->80 ->93 (replace 96 with 93)

->10 ->54 ->93 (update 80)

->3   ->54 ->93 

->3   ->26 ->93

->4   ->26 ->93 (Note: the smallest number 3 is still smaller than new comer 4, the rule is to replace 3 with new comer 4)

->4   ->26 ->37

Finally, return the length of the arraylist which is the result.

 

posted @ 2014-09-26 13:59  zmiao  阅读(133)  评论(0编辑  收藏  举报