Day52| 300 最长递增子序列 674 最长连续递增序列 718 最长重复子数组

目录

[300 最长递增子序列](#300 最长递增子序列)

[674 最长连续递增序列](#674 最长连续递增序列)

[718 最长重复子数组](#718 最长重复子数组)


300 最长递增子序列

cpp 复制代码
class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        if(nums.size() <= 1) return nums.size();
        int result = 0;
        //到达当前下标值的最大子序列长度
        vector<int> dp(nums.size() + 1, 1);

        for(int i = 1; i < nums.size(); i++){
            for(int j = 0; j < i; j++){
                if(nums[i] > nums[j]){
                    dp[i] = max(dp[j] + 1, dp[i]);
                }
            }
            if (dp[i] > result) result = dp[i];
        }
        return result;
    }
};

674 最长连续递增序列

cpp 复制代码
class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {;
        int result = 1;
        vector<int> dp(nums.size() + 1, 1);

        for (int i = 1; i < nums.size(); i++) {
            if (nums[i-1] < nums[i]) {
                dp[i] = dp[i - 1] + 1;
            }
            if (result < dp[i]) result = dp[i];
        }
        return result;
    }
};

718 最长重复子数组

cpp 复制代码
class Solution {
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
        int result = 0;
        vector<vector<int>> dp(nums1.size() + 1, vector<int>(nums2.size() + 1, 0));

        for(int i = 1; i <= nums1.size(); i++){
            for(int j = 1; j <= nums2.size(); j++){
                if(nums1[i-1] == nums2[j-1]){
                    dp[i][j] = dp[i-1][j-1] + 1;
                }
                result = max(dp[i][j], result);
            }
        }
        return result;
    }
};
相关推荐
秋难降10 分钟前
LRU缓存算法(最近最少使用算法)——工业界缓存淘汰策略的 “默认选择”
数据结构·python·算法
tkevinjd17 分钟前
图论\dp 两题
leetcode·动态规划·图论
CoovallyAIHub2 小时前
线性复杂度破局!Swin Transformer 移位窗口颠覆高分辨率视觉建模
深度学习·算法·计算机视觉
点云SLAM2 小时前
Eigen中Dense 模块简要介绍和实战应用示例(最小二乘拟合直线、协方差矩阵计算和稀疏求解等)
线性代数·算法·机器学习·矩阵·机器人/slam·密集矩阵与向量·eigen库
Jayyih2 小时前
嵌入式系统学习Day19(数据结构)
数据结构·学习
renhongxia12 小时前
大模型微调RAG、LORA、强化学习
人工智能·深度学习·算法·语言模型
DdduZe3 小时前
8.19作业
数据结构·算法
PyHaVolask3 小时前
链表基本运算详解:查找、插入、删除及特殊链表
数据结构·算法·链表
高山上有一只小老虎3 小时前
走方格的方案数
java·算法