首页 > 其他 > 详细

leetcode 300

时间:2019-03-30 13:30:47      阅读:143      评论:0      收藏:0      [点我收藏+]

原题链接

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        if(nums.empty()) return 0;
        int len = nums.size();
        vector<int> dp(len,1);
        int max = 1;
        for(int i = 1;i < len;++i){
            for(int j = 0;j < i;++j){
                if(nums[i] > nums[j]){
                    dp[i] = dp[j] + 1 > dp[i] ? dp[j] + 1 : dp[i] ;                                     
                }
                if(dp[i] > max) max = dp[i];
            }
        }
        return max;
    }
};

讨论区的人才有更好的解决方案,利用 STL 的 lower_bound 函数,效率提升不少。

class Solution{
public:
    int lengthOfLIS(vector<int>& nums) {
    vector<int> ans;
    for (int a : nums)
        if (ans.size() == 0 || a > ans.back()) ans.push_back(a);
        else *lower_bound(ans.begin(), ans.end(), a) = a;
    return ans.size();
}    
};

leetcode 300

原文:https://www.cnblogs.com/walnuttree/p/10626590.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!