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;
    }
};
相关推荐
草莓工作室2 分钟前
数据结构8:栈
c语言·数据结构
Han.miracle8 分钟前
数据结构——排序的学习(一)
java·数据结构·学习·算法·排序算法
爱coding的橙子15 分钟前
每日算法刷题Day76:10.19:leetcode 二叉树12道题,用时3h
算法·leetcode·职场和发展
晚枫~1 小时前
图论基础:探索节点与关系的复杂网络
网络·数据结构·图论
liu****1 小时前
20.哈希
开发语言·数据结构·c++·算法·哈希算法
夏鹏今天学习了吗2 小时前
【LeetCode热题100(47/100)】路径总和 III
算法·leetcode·职场和发展
smj2302_796826522 小时前
解决leetcode第3721题最长平衡子数组II
python·算法·leetcode
m0_626535202 小时前
力扣题目练习 换水问题
python·算法·leetcode
第六五2 小时前
DPC和DPC-KNN算法
人工智能·算法·机器学习
一匹电信狗2 小时前
【LeetCode_160】相交链表
c语言·开发语言·数据结构·c++·算法·leetcode·stl