最长递增子序列

参考<Intruction to algorithm - a creative approach> 6.11

 1 int lengthOfLIS(vector<int>& nums) 
 2     {
 3         vector<int> BIS(nums.size() + 1, INT_MAX);
 4         int len = 0;
 5         
 6         for (int i = 0; i < nums.size(); i++)
 7         {
 8             for (int k = BIS.size() - 1; k >= 0; k--)
 9             {
10                 if ((nums[i] > BIS[k]) && (nums[i] < BIS[k + 1]))
11                 {
12                     BIS[k + 1] = nums[i];
13                 }
14             }
15             if (BIS[0] > nums[i])
16             {
17                 BIS[0] = nums[i];
18             }
19         }
20         
21         for (len = 0; len < BIS.size(); len++)
22             if (BIS[len] >= INT_MAX)
23                 break;
24                 
25         return len;
26     }

 

posted on 2016-06-29 09:02  ym65536  阅读(194)  评论(0编辑  收藏  举报