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

最长递增子序列

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;
    }
}
相关推荐
啦啦啦_99991 分钟前
1. 线性回归之 导数偏导数
算法·矩阵·线性回归
踩坑记录3 分钟前
leetcode hot100 5. 最长回文子串 中心扩展法 medium
leetcode
itzixiao23 分钟前
L1-058 6翻了(15分)[java][python]
java·开发语言·python·算法
念何架构之路24 分钟前
数组和切片实战
数据结构·算法·排序算法
重生之我是Java开发战士26 分钟前
【数据结构】AVL树解析
数据结构·算法
小π军29 分钟前
STL之multiset 常见API介绍
数据结构·c++·算法
踩坑记录29 分钟前
leetcode hot100 1143. 最长公共子序列 mediuim 递归优化
leetcode
研究点啥好呢31 分钟前
Momenta算法工程师面试题精选:10道高频考题+答案解析
人工智能·算法·求职招聘·面试笔试
Resistance丶未来31 分钟前
DeepSeek-V4 新手快速上手指南
数据结构·python·gpt·算法·机器学习·claude·claude 4.6
无限进步_40 分钟前
【C++】寻找数组中出现次数超过一半的数字:三种解法深度剖析
开发语言·c++·git·算法·leetcode·github·visual studio