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;
    }
};
相关推荐
努力写代码的熊大18 分钟前
链式二叉树数据结构(递归)
数据结构
yi.Ist18 分钟前
数据结构 —— 键值对 map
数据结构·算法
爱学习的小邓同学18 分钟前
数据结构 --- 队列
c语言·数据结构
s1533521 分钟前
数据结构-顺序表-猜数字
数据结构·算法·leetcode
闻缺陷则喜何志丹22 分钟前
【前缀和 BFS 并集查找】P3127 [USACO15OPEN] Trapped in the Haybales G|省选-
数据结构·c++·前缀和·宽度优先·洛谷·并集查找
Coding小公仔24 分钟前
LeetCode 8. 字符串转换整数 (atoi)
算法·leetcode·职场和发展
GEEK零零七30 分钟前
Leetcode 393. UTF-8 编码验证
算法·leetcode·职场和发展·二进制运算
DoraBigHead2 小时前
小哆啦解题记——异位词界的社交网络
算法
木头左3 小时前
逻辑回归的Python实现与优化
python·算法·逻辑回归
lifallen7 小时前
Paimon LSM Tree Compaction 策略
java·大数据·数据结构·数据库·算法·lsm-tree