[leetcode]最长递增序列

 

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        int n=nums.size();
        if(n==0) return 0;
        
        vector<int> maxv(n+1);
        maxv[0]=INT_MIN;
        maxv[1]=nums[0];
        
        vector<int> lis(n);
        lis[0]=1;
        
        int nMaxLen=1;
        
        for(int i=1;i<n;i++)
        {
            int j;
            for(j=nMaxLen;j>=0;j--)
            {
                if(nums[i]>maxv[j])
                {
                    lis[i]=j+1;
                    break;
                }
            }
            if(j==nMaxLen)
            {
                maxv[j+1]=nums[i];
                nMaxLen=j+1;
            }
            else if(nums[i]<maxv[j+1])
            {
                maxv[j+1]=nums[i];
            }
        }
        for(auto &x: maxv)
            cout<<x<<" ";
        cout<<endl;
        for(auto &x:lis)
            cout<<x<<" ";
        cout<<endl;
        return nMaxLen;
    }
};

 

posted @ 2016-08-06 21:25  hopskin1  阅读(595)  评论(0编辑  收藏  举报