算法训练营第四十天(9.1)| 动态规划Part11:最长子序列系列

Leecode 300.最长递增子序列

题目地址:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

题目类型:最长子序列

cpp 复制代码
class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        int n = nums.size();
        // dp[i]代表到第i个元素时的最长子序列长度
        vector<int> dp(n, 1);
        int res = 1;
        for (int i = 1; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                if (nums[i] > nums[j]) dp[i] = max(dp[i], dp[j] + 1);
            }
            res = max(res, dp[i]);
        }
        return res;
    }
};

Leecode 674.最长连续递增序列

题目地址:​​​​​​​力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

题目类型:最长子序列

cpp 复制代码
class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        int n = nums.size();
        int res = 1, cur = 1;
        for (int i = 1; i < n; ++i) {
            if (nums[i] > nums[i - 1]) {
                cur++;
                res = max(cur, res);
            }
            else {
                cur = 1;
            }
        }
        return res;
    }
};

Leecode 718.最长重复子数组

题目地址:​​​​​​​力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

题目类型:最长子序列

cpp 复制代码
class Solution {
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
        int m = nums1.size(), n = nums2.size();
        int result = 0;
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= n; ++j) {
                if (nums1[i - 1] == nums2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
                if (dp[i][j] > result) result = dp[i][j];
            }
        }
        return result;
    }
};
相关推荐
苦藤新鸡3 分钟前
14.合并区间(1,3)(2,5)=(1,5)
c++·算法·leetcode·动态规划
程序员-King.12 分钟前
day145—递归—二叉树的右视图(LeetCode-199)
算法·leetcode·二叉树·递归
漫随流水16 分钟前
leetcode算法(112.路径总和)
数据结构·算法·leetcode·二叉树
过期的秋刀鱼!24 分钟前
机器学习-带正则化的成本函数-
人工智能·python·深度学习·算法·机器学习·逻辑回归
ScilogyHunter26 分钟前
前馈/反馈控制是什么
算法·控制
_OP_CHEN37 分钟前
【算法基础篇】(四十八)突破 IO 与数值极限:快速读写 +__int128 实战指南
c++·算法·蓝桥杯·算法竞赛·快速读写·高精度算法·acm/icpc
程序员泠零澪回家种桔子41 分钟前
RAG自查询:让AI精准检索的秘密武器
人工智能·后端·算法
企鹅侠客42 分钟前
第24章—数据结构篇:skiplist原理与实现解析
数据结构·skiplist
糖葫芦君1 小时前
TRPO-trust region policy optimization论文讲解
人工智能·算法·机器学习·强化学习
HaiLang_IT1 小时前
基于RepVGG与注意力机制的手写潦草汉字识别算法研究
算法