算法训练营第五十二天|300.最长递增子序列 674. 最长连续递增序列 718. 最长重复子数组

目录

  • Leetcode300.最长递增子序列
  • [Leetcode674. 最长连续递增序列](#Leetcode674. 最长连续递增序列)
  • [Leetcode718. 最长重复子数组](#Leetcode718. 最长重复子数组)

Leetcode300.最长递增子序列

文章链接:代码随想录

题目链接:300.最长递增子序列

思路:数组存在就至少为一,dp元素初始化为1

cpp 复制代码
class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        if (nums.size() == 1) return 1;
        vector<int> dp(nums.size(), 1);
        int result = 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[i], dp[j] + 1);
            }
            result = result > dp[i] ? result : dp[i];
        }
        return result;
    }
};

Leetcode674. 最长连续递增序列

文章链接:代码随想录

题目链接:674. 最长连续递增序列

思路:连续的话,比较相邻元素即可。

cpp 复制代码
class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        vector<int> dp(nums.size(), 1);
        int result = 1;
        for (int i = 1; i < nums.size(); i++){
            if (nums[i] > nums[i - 1]) dp[i] = dp[i - 1] + 1;
            result = result > dp[i] ? result : dp[i];
        }
        return result;
    }
};

Leetcode718. 最长重复子数组

文章链接:代码随想录

题目链接:718. 最长重复子数组

思路:二维数组,创建数组时多建一层是为了避免初始化,否则就得在循环前先初始化一遍dp[i][0]和dp[0][j]。

cpp 复制代码
class Solution {
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
        vector<vector<int>> dp(nums1.size() + 1, vector<int>(nums2.size() + 1));
        int result = 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 = result > dp[i][j] ? result : dp[i][j];
            }
        }
        return result;
    }
};

滚动(一维)数组,不等要有赋0操作。理论上说不等也最起码有dp[j - 1]个子字符串相等,但是这个值会影响下一层的判断,若下一层的两元素相等,则会得出错误结果,故不等需要赋0。而每个相等字符串的长度都会有一个元素记录过,无需担心漏记。

和背包问题同样 j 的后序遍历是为了避免错误累加。

cpp 复制代码
class Solution {
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
        vector<int> dp(nums2.size() + 1);
        int result = 0;
        for (int i = 1; i <= nums1.size(); i++){
            for (int j = nums2.size(); j > 0; j--){
                if (nums1[i - 1] == nums2[j - 1]){
                    dp[j] = dp[j - 1] + 1;
                }else dp[j] = 0;
                result = result > dp[j] ? result : dp[j];
            }
        }
        return result;
    }
};

第五十二天打卡,加油!!!

相关推荐
~~李木子~~1 天前
贪心算法实验1
算法·ios·贪心算法
·云扬·1 天前
【LeetCode Hot 100】 136. 只出现一次的数字
算法·leetcode·职场和发展
Xiaochen_121 天前
有边数限制的最短路:Bellman-Ford 算法
c语言·数据结构·c++·程序人生·算法·学习方法·最简单的算法理解
大胆飞猪1 天前
递归、剪枝、回溯算法---全排列、子集问题(力扣.46,78)
算法·leetcode·剪枝
Kisorge1 天前
【电机控制】基于STM32F103C8T6的二轮平衡车设计——LQR线性二次线控制器(算法篇)
stm32·嵌入式硬件·算法
铭哥的编程日记1 天前
深入浅出蓝桥杯:算法基础概念与实战应用(二)基础算法(下)
算法·职场和发展·蓝桥杯
Swift社区1 天前
LeetCode 421 - 数组中两个数的最大异或值
算法·leetcode·职场和发展
cici158741 天前
基于高光谱成像和偏最小二乘法(PLS)的苹果糖度检测MATLAB实现
算法·matlab·最小二乘法
StarPrayers.1 天前
自蒸馏学习方法
人工智能·算法·学习方法
大锦终1 天前
【动规】背包问题
c++·算法·动态规划