Day43 >> 300.最长递增子序列 + 674. 最长连续递增序列+ 718. 最长重复子数组

代码随想录-动态规划Part10

300.最长递增子序列

java 复制代码
class Solution {
    public int lengthOfLIS(int[] nums) {
        if (nums.length <= 1) return nums.length;
        int[] dp = new int[nums.length];
        int res = 1;
        Arrays.fill(dp, 1);
        for (int i = 1; i < dp.length; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            res = Math.max(res, dp[i]);
        }
        return res;
    }
}

674. 最长连续递增序列

java 复制代码
class Solution {
    public int findLengthOfLCIS(int[] nums) {
        int beforeOneMaxLen = 1, currentMaxLen = 0;
        int res = 1;
        for (int i = 1; i < nums.length; i ++) {
            currentMaxLen = nums[i] > nums[i - 1] ? beforeOneMaxLen + 1 : 1;
            beforeOneMaxLen = currentMaxLen;
            res = Math.max(res, currentMaxLen);
        }
        return res;
    }
}

718. 最长重复子数组

java 复制代码
class Solution {
    public int findLength(int[] nums1, int[] nums2) {
        int[] dp = new int[nums2.length + 1];
        int result = 0;

        for (int i = 1; i <= nums1.length; i++) {
            for (int j = nums2.length; j > 0; j--) {
                if (nums1[i - 1] == nums2[j - 1]) {
                    dp[j] = dp[j - 1] + 1;
                } else {
                    dp[j] = 0;
                }
                result = Math.max(result, dp[j]);
            }
        }
        return result;
    }
}
相关推荐
小李飞刀李寻欢9 小时前
DeepSeek V3 版本模型结构分析
算法·大模型·deepseek
某不知名網友9 小时前
C++ 七大排序算法完整讲解
java·算法·排序算法
得物技术10 小时前
得物推荐系统诊断 Agent:从 “调接口” 到 “会思考”|AICon 演讲整理
人工智能·算法·架构
Lugas10 小时前
为啥说男生找对象尽量在25岁前找到?
算法
MrZhao40010 小时前
从能跑到可用:一个 Agent Harness 还差哪些工程闭环?
算法
QN1幻化引擎10 小时前
Gravity-Anchored Cognitive Field Architecture: The DalinX V8/V10 Implementation
java·前端·算法
学计算机的计算基11 小时前
LeetCode 图论四题精讲:BFS、拓扑排序、Trie 树的模板与优化
java·笔记·算法
浩瀚地学11 小时前
【面试算法笔记】0202-链表-基本功能实现
java·经验分享·笔记·算法·面试
tkevinjd11 小时前
416分割等和子集
java·python·算法·leetcode·职场和发展
Keven_1111 小时前
算法札记:Tarjan与拓扑序(Topo)的关系
算法·拓扑·tarjan