leetcode-300. 最长递增子序列

300. 最长递增子序列

给你一个整数数组 nums ,
找到其中最长严格递增子序列的长度。 子序列是由数组派生而来的序列,
删除(或不删除)数组中的元素而不改变其余元素的顺序。
例如,[
3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。   示例 1: 输入:nums = [10,9,2,5,3,7,101,18] 输出:4 解释:最长递增子序列是 [2,3,7,101],因此长度为 4 。 示例 2: 输入:nums = [0,1,0,3,2,3] 输出:4 示例 3: 输入:nums = [7,7,7,7,7,7,7] 输出:1   提示: 1 <= nums.length <= 2500 -104 <= nums[i] <= 104   进阶: 你可以设计时间复杂度为 O(n2) 的解决方案吗? 你能将算法的时间复杂度降低到 O(n log(n)) 吗? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/longest-increasing-subsequence 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

先思考,再看答案

 1 class Solution {
 2 public:
 3     int lengthOfLIS(vector<int>& nums) {
 4         if(nums.size()==0)
 5         {
 6             return 0;
 7         }
 8         vector<int> dp(nums.size(),1);
 9         for(int i=0;i<nums.size();++i)
10         {
11             for(int j=0;j<i;j++)
12             {
13                 if(nums[i]>nums[j])
14                 {
15                     dp[i] = max(dp[i], dp[j]+1);
16                 }
17             }
18         }
19 
20         int res = 0;
21         for(auto val : dp)
22         {
23             //printf("val:%d ,",val);            
24             res = max(res, val);
25         }
26         //cout <<endl;
27         return res;
28     }
29 };
View Code

 

posted @ 2020-12-28 08:58  He_LiangLiang  阅读(147)  评论(0编辑  收藏  举报