1. 程式人生 > >300 Longest Increasing Subsequence

300 Longest Increasing Subsequence

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
    int length=nums.size();
    if(length==0){
        return 0;
    }
    int cont=0;
    int dp[length+1];
    dp[++cont]=nums[0];
        for(int i=1;i<length;i++)
        {
          if(nums[i]>dp[cont])
          {
              dp[++cont]=nums[i];
          }
          else
          {
              int mid,l=1,r=cont;
              while(l<=r)
              {
                 mid=(l+r)/2;
                 if(dp[mid]<nums[i])
                 {
                     l=mid+1;
                 }
                 else
                 {
                     r=mid-1;
                 }
              }
              dp[l]=nums[i];
          }
        }
        return cont;
    }
};