This question requires that a number be returned, which represents the longest incrementing subsequence in an array. Of course, this sequence is not required to be continuous. For example, there is an array: {1, 3, 5, 7, 2, 9}, then the longest incrementing sub-sequence of this array is 5, that is, 1, 3, 5, 7, 9
The idea of solving this problem is: as long as the number behind is greater than the maximum value of the first ascending subsequence, it must be greater than all the previous sequences. Since we need to know the previously stored sequence, here we need an auxiliary array, which stores the "smallest continuous increment sub-array". When operating this array, we need to follow two rules:
If the secondary array is empty, directly add the number of the original array to it.
1. if the number in the original array is greater than the maximum number in the secondary array (that is, the last number), you can directly put the number in the original array, and add the incremental Sequence Value to 1.
2. if the number in the original array is smaller than or equal to the maximum number in the secondary array, we need to replace the value in the secondary array because we need the smallest continuous increment sub-array, we can use the binary search method (because the secondary array is ordered) to find the number that is exactly bigger than the current number. just replace it with it.
Finally, return the length of the secondary array:
Function declaration:
/* 2.16 longest incrementing subsequence */INT dutbinfindforlrs (int *, Int, INT); int dutlrs (int *, INT );
Source code:
/* Binary Search */INT dutbinfindforlrs (int * a, int size, int v) {If (! A | size <= 0) Return-1; int low = 0; int high = size-1; while (low <= high) {int mid = low + (high-low)/2; If (V> = A [Mid]) Low = Mid + 1; elsehigh = mid-1 ;} return low;} int dutlrs (int * a, int size) {If (! A | size <= 0) return 0; int Len = 1; int * TMP = new int [size]; TMP [0] = A [0]; for (INT I = 1; I <size; ++ I) {if (a [I]> TMP [Len-1]) TMP [Len ++] = A [I]; else {int Pos = dutbinfindforlrs (TMP, Len, a [I]); TMP [POS] = A [I];} Delete [] TMP; TMP = NULL; return Len ;}
Programming beauty 2.16 longest incremental subsequence