算法训练营第四十天(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;
    }
};
相关推荐
爱代码的小黄人2 小时前
深入解析系统频率响应:通过MATLAB模拟积分器对信号的稳态响应
开发语言·算法·matlab
是僵尸不是姜丝5 小时前
每日算法:洛谷U535992 J-C 小梦的宝石收集(双指针、二分)
c语言·开发语言·算法
寒页_7 小时前
2025年第十六届蓝桥杯省赛真题解析 Java B组(简单经验分享)
java·数据结构·经验分享·算法·蓝桥杯
smile-yan7 小时前
拓扑排序 —— 2. 力扣刷题207. 课程表
数据结构·算法·图论·拓扑排序
空雲.8 小时前
牛客周赛88
数据结构·c++·算法
深度学习算法与自然语言处理8 小时前
单卡4090微调大模型 DeepSeek-R1-32B
深度学习·算法·大模型·微调·transformer·面试题
Y1nhl8 小时前
基础算法:滑动窗口_python版本
开发语言·python·算法·力扣·滑动窗口
烟锁池塘柳08 小时前
【数学建模】(智能优化算法)鲸鱼优化算法(Whale Optimization Algorithm)详解与应用
算法·数学建模
地平线开发者9 小时前
【征程 6】工具链 VP 示例中 Cmakelists 解读
算法·自动驾驶
邪神与厨二病9 小时前
2025蓝桥杯python A组题解
数据结构·c++·python·算法·蓝桥杯·单调栈·反悔贪心