Rise longest sequence (LIS: Longest Increasing Subsequence)

 

Example:

Input: [10,9,2,5,3,7,101,18]
Output: 4 
explained: the longest sequence is increase  [2,3,7,101],its length 4. 

From the Internet to find a piece of code (I changed from a C ++ java version), the original author succinctly explain very clearly. I will read the general algorithm question their own ideas to write again, but this algorithm code is really simple, but great idea, it is no longer write their own again a.
class Solution {
 public : 
         int lengthOfLIS ( int * the nums) {
         / * * 
        dp [I]:. all length-increasing subsequence i + 1, the minimum of that sequence mantissa 
        By definition dp array must be an incremental array, can be represented by the length of the longest maxL incremental sequence. 
        array iteration, each of the corresponding number of successively determined num be inserted dp array location: 
            1. num> dp [maxL], it represents all known num than increasing sequence mantissa are large, the added num dp 
               end of the array, and the longest increasing sequence length plus maxL. 1 
            2. DP [-I. 1] <NUM <DP = [I], only updates the corresponding DP [I] 
        * * / 
        int maxL = 0 ;
         int n-= the sizeof (the nums) / the sizeof ( int );
         int* DP = new new  int [n-+ . 5 ];
         for ( int I = 0 ; I <n-; I ++ ) {
             int NUM = the nums [I];
             // binary search, can also call the library functions such binary_search 
            int Low = 0 , = High maxL;
             the while (Low < High) {
                 int MID = (High + Low) / 2 ;
                 IF (DP [MID] < NUM) 
                    Low = MID + . 1 ;
                 the else 
                    High= mid;
            }
            dp[low] = num;
            if (low == maxL)
                maxL++;
        }
        return maxL;
    }
};

 

Guess you like

Origin www.cnblogs.com/chen9510/p/11481181.html