代码随想录算法训练营|五十一天

最长递增子序列

300. 最长递增子序列 - 力扣(LeetCode)

递推公式:

有点像双指针的操作,例如{2,5,6,4,3}(写不出来,画图)

cs 复制代码
public class Solution {
    public int LengthOfLIS(int[] nums) {
        if (nums.Length <= 1) return nums.Length;
        int[] dp = new int[nums.Length];
        int result = 0;
        for (int i = 0; i < nums.Length; i++) {
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) dp[i] = Math.Max(dp[i], dp[j] + 1);
            }
            result = Math.Max(result, dp[i]);
        }
        return result;
    }
}

最长连续递增序列

674. 最长连续递增序列 - 力扣(LeetCode)

数组不连续递增就重新计数

cs 复制代码
public class Solution {
    public int FindLengthOfLCIS(int[] nums) {
        if(nums.Length <= 1){return nums.Length;}
        int[] dp = new int[nums.Length];
        int result = 0;
        for(int i=0;i<nums.Length;i++){
            dp[i] = 1;
            if(i>0 && nums[i]>nums[i-1]){
                dp[i] = dp[i-1]+1;
            }
            if(dp[i]>result)result = dp[i];
        }
        
        return result;
    }
}

最长重复子数组

718. 最长重复子数组 - 力扣(LeetCode)

这个图就很清楚递推怎么来的

cs 复制代码
public class Solution {
    public int FindLength(int[] nums1, int[] nums2) {
        int[,] dp = new int[nums1.Length + 1, nums2.Length + 1];
        int result = 0;
        for (int i = 1; i <= nums1.Length; i++) {
            for (int j = 1; j <= nums2.Length; 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;
    }
}
相关推荐
longlongzihan8 小时前
LeetCode 17电话号码的字母组合:回溯算法(DFS)详解
c++·算法·leetcode·深度优先
封印师请假去地球钓鱼8 小时前
边解边变的问题:从“决策依赖“一词出发
人工智能·算法
今晚打老虎9 小时前
c++之提高A(前缀和)(第三课)
数据结构·c++·算法
不会就选b10 小时前
算法日常・每日刷题--<贪心>26
数据结构·算法
hetao173383711 小时前
2026-09-29 hetao1733837 的刷题记录
c++·算法
欣欣之王来了12 小时前
AI合规专项:AI算法透明度的合规要求
人工智能·算法
wzdark12 小时前
分块思想在算法优化中的实践与应用4
算法
Because_of_Her112 小时前
dfs-洪水填充技巧
笔记·算法·深度优先·洪水填充
小静AI工程实验室13 小时前
RSA 算法详解:从模幂原理到 OAEP 加密、PSS 签名与 Python 实验
python·算法
xzal1213 小时前
GESP Python笔记
笔记·python·算法