算法训练营第四十天(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;
    }
};
相关推荐
2501_924890528 分钟前
商超场景徘徊识别误报率↓79%!陌讯多模态时序融合算法落地优化
java·大数据·人工智能·深度学习·算法·目标检测·计算机视觉
艾醒16 分钟前
大模型面试题剖析:模型微调和蒸馏核心技术拆解与考点梳理
算法
艾醒1 小时前
大模型面试题剖析:微调与 RAG 技术的选用逻辑
算法
NAGNIP2 小时前
一文弄懂MOE
算法
NAGNIP2 小时前
一文搞懂微调技术的发展与演进
算法
Vect__2 小时前
链表漫游指南:C++ 指针操作的艺术与实践
数据结构·c++·链表
古译汉书2 小时前
蓝桥杯算法之基础知识(2)——Python赛道
数据结构·python·算法·蓝桥杯
地平线开发者2 小时前
LLM 中增量解码与模型推理解读
算法·自动驾驶
.Vcoistnt2 小时前
Codeforces Round 1043 (Div. 3)(A-E)
数据结构·算法
野犬寒鸦2 小时前
力扣hot100:搜索二维矩阵与在排序数组中查找元素的第一个和最后一个位置(74,34)
java·数据结构·算法·leetcode·list