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

最长递增子序列

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;
    }
}
相关推荐
Themberfue4 分钟前
基础算法之双指针--Java实现(下)--LeetCode题解:有效三角形的个数-查找总价格为目标值的两个商品-三数之和-四数之和
java·开发语言·学习·算法·leetcode·双指针
陈序缘29 分钟前
LeetCode讲解篇之322. 零钱兑换
算法·leetcode·职场和发展
-$_$-32 分钟前
【LeetCode HOT 100】详细题解之二叉树篇
数据结构·算法·leetcode
大白飞飞34 分钟前
力扣203.移除链表元素
算法·leetcode·链表
学无止境\n1 小时前
[C语言]指针和数组
c语言·数据结构·算法
黄俊懿1 小时前
【深入理解SpringCloud微服务】手写实现各种限流算法——固定时间窗、滑动时间窗、令牌桶算法、漏桶算法
java·后端·算法·spring cloud·微服务·架构
新缸中之脑1 小时前
Llama 3.2 安卓手机安装教程
前端·人工智能·算法
夜雨翦春韭1 小时前
【代码随想录Day29】贪心算法Part03
java·数据结构·算法·leetcode·贪心算法
Curry_Math2 小时前
Acwing 区间DP & 计数类DP
算法
Tisfy2 小时前
LeetCode 1928.规定时间内到达终点的最小花费:动态规划
算法·leetcode·动态规划·