算法初试

DP算法-初试

最长上升子序问题

给定一个无序的整数数组,找到其中最长上升子序列的长度。
示例:
输入: [10,9,2,5,3,7,101,18]
输出: 4
解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。

基本思路

拿到这个问题最先想到的就是循环遍历,复杂度为O(n^2),后续会进行改进。

class Solution {
public:
int lengthOfLIS(std::vector& nums) {

    int max_count = std::numeric_limits<int>::min();

    int count = 1;
    int value = 0;
    int n = nums.size();
    for (int i = 0; i < n; ++i) {
        value = nums[i];
        for (int j = (i + 1); j < n; ++j)
        {
            if (nums[j] > value) {
                count++;
                value = nums[j];
            }
        }
        if (count > max_count) max_count = count;
        count = 1;
    }

    return max_count;
}

};



发布了2 篇原创文章 · 获赞 0 · 访问量 3

猜你喜欢

转载自blog.csdn.net/weixin_47017567/article/details/105477534
今日推荐