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;
    }
}
相关推荐
@––––––11 分钟前
力扣hot100—系列2-多维动态规划
算法·leetcode·动态规划
xsyaaaan23 分钟前
代码随想录Day31动态规划:1049最后一块石头的重量II_494目标和_474一和零
算法·动态规划
Jay Kay1 小时前
GVPO:Group Variance Policy Optimization
人工智能·算法·机器学习
Epiphany.5561 小时前
蓝桥杯备赛题目-----爆破
算法·职场和发展·蓝桥杯
YuTaoShao2 小时前
【LeetCode 每日一题】1653. 使字符串平衡的最少删除次数——(解法三)DP 空间优化
算法·leetcode·职场和发展
茉莉玫瑰花茶2 小时前
C++ 17 详细特性解析(5)
开发语言·c++·算法
cpp_25012 小时前
P10570 [JRKSJ R8] 网球
数据结构·c++·算法·题解
cpp_25012 小时前
P8377 [PFOI Round1] 暴龙的火锅
数据结构·c++·算法·题解·洛谷
uesowys2 小时前
Apache Spark算法开发指导-Factorization machines classifier
人工智能·算法
季明洵3 小时前
C语言实现单链表
c语言·开发语言·数据结构·算法·链表