代码随想录算法训练营day52 300.递增子序列 674.最长连续递增子序列 718.最长重复子数组

题目链接300.递增子序列

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

题目链接674.最长连续递增子序列

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

题目链接718.最长重复子数组

复制代码
class Solution {
    public int findLength(int[] nums1, int[] nums2) {
        int res = 0;
        int[][] dp = new int[nums1.length+1][nums2.length+1];
        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] = Math.max(dp[i][j], dp[i-1][j-1] + 1);
                }
                res = Math.max(res, dp[i][j]);
            }
        }
        return res;
    }
}
相关推荐
.道阻且长.9 小时前
2.LeetCode算法习题讲解--双指针--复写零
算法·leetcode·职场和发展
To_OC10 小时前
LC 438 找到所有字母异位词:暴力超时后,我靠滑动窗口一招搞定
javascript·算法·leetcode
Forever Nore13 小时前
学完C语言力扣第一题做不来正常吗
数据结构·算法
hansang_IR14 小时前
【题解】LC:倍增 / 区间并查集(Range Parallel Unionfind)
c++·算法·并查集
Tisfy15 小时前
LeetCode 3731.找出缺失的元素:哈希 / 排序
算法·leetcode·哈希算法·排序·哈希表
lucas_AI16 小时前
Q-CueGraph:你的多模态大模型会 zoom,但真的知道该看哪儿吗?
人工智能·算法
kaixin_啊啊16 小时前
test_机器学习算法学习
学习·算法·机器学习
liulilittle16 小时前
MOE路由:路由(logits: top-k/8)
c++·人工智能·算法·机器学习·llm
旖旎夜光16 小时前
LeetCode 11:盛最多水的容器(双指针问题) —— 题解
数据结构·c++·算法·leetcode·双指针