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

最长递增子序列

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;
    }
}
相关推荐
快去睡觉~2 小时前
力扣73:矩阵置零
算法·leetcode·矩阵
岁忧2 小时前
(nice!!!)(LeetCode 每日一题) 679. 24 点游戏 (深度优先搜索)
java·c++·leetcode·游戏·go·深度优先
小欣加油2 小时前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode
猿究院--王升5 小时前
jvm三色标记
java·jvm·算法
一车小面包5 小时前
逻辑回归 从0到1
算法·机器学习·逻辑回归
tt5555555555557 小时前
字符串与算法题详解:最长回文子串、IP 地址转换、字符串排序、蛇形矩阵与字符串加密
c++·算法·矩阵
元亓亓亓8 小时前
LeetCode热题100--101. 对称二叉树--简单
算法·leetcode·职场和发展
不会学习?8 小时前
算法03 归并分治
算法
NuyoahC9 小时前
笔试——Day43
c++·算法·笔试
2301_821919929 小时前
决策树8.19
算法·决策树·机器学习