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

最长递增子序列

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;
    }
}
相关推荐
西红柿维生素35 分钟前
JVM相关总结
java·jvm·算法
ChillJavaGuy2 小时前
常见限流算法详解与对比
java·算法·限流算法
sali-tec2 小时前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
你怎么知道我是队长4 小时前
C语言---循环结构
c语言·开发语言·算法
艾醒4 小时前
大模型面试题剖析:RAG中的文本分割策略
人工智能·算法
纪元A梦6 小时前
贪心算法应用:K-Means++初始化详解
算法·贪心算法·kmeans
_不会dp不改名_6 小时前
leetcode_21 合并两个有序链表
算法·leetcode·链表
mark-puls6 小时前
C语言打印爱心
c语言·开发语言·算法
Python技术极客6 小时前
将 Python 应用打包成 exe 软件,仅需一行代码搞定!
算法
吃着火锅x唱着歌6 小时前
LeetCode 3302.字典序最小的合法序列
leetcode