算法修炼Day52|● 300.最长递增子序列 ● 674. 最长连续递增序列 ● 718. 最长重复子数组

LeetCode:300.最长递增子序列

300. 最长递增子序列 - 力扣(LeetCode)

1.思路

dpi的状态表示以numsi为结尾的最长递增子序列的个数。

dpi有很多个,选择其中最大的dp[i]=Math.max(dp[j]+1,dp[i])

2.代码实现

复制代码
 1class Solution {
 2    public int lengthOfLIS(int[] nums) {
 3        int[] dp = new int[nums.length];
 4        Arrays.fill(dp, 1);
 5        for (int i = 1; i < nums.length; i++) {
 6            for (int j = 0; j < i; j++) {
 7                if (nums[j] < nums[i]) {
 8                    dp[i] = Math.max(dp[j] + 1, dp[i]);
 9                }
10            }
11        }
12        int res = 0;
13        for (int i = 0; i < nums.length; i++) {
14            res = Math.max(res, dp[i]);
15        }
16        return res;
17    }
18}

3.复杂度分析

时间复杂度:O(n).

空间复杂度:O(n).

LeetCode: 674. 最长连续递增序列

674. 最长连续递增序列 - 力扣(LeetCode)

1.思路

后一个状态是由当前状态推出来的,注意边界值...

2.代码实现

复制代码
 1class Solution {
 2    public int findLengthOfLCIS(int[] nums) {
 3        int[] dp = new int[nums.length];
 4        Arrays.fill(dp, 1);
 5
 6        for (int i = 0; i < nums.length - 1; i++) {        
 7
 8            if (nums[i + 1] > nums[i]) {
 9                dp[i + 1] = dp[i] + 1;
10            }
11        }
12        int res = 0;
13        for (int i = 0; i < dp.length; i++) {
14            res = Math.max(dp[i], res);
15        }
16        return res;
17    }
18}

3.复杂度分析

时间复杂度:O(n).

空间复杂度:O(n).

LeetCode:718. 最长重复子数组

718. 最长重复子数组 - 力扣(LeetCode)

1.思路

动规dpij定义很关键,当前状态需要前一个状态推导出来。

2.代码实现

复制代码
 1// 暴力解法
 2class Solution {
 3    public int findLength(int[] nums1, int[] nums2) {
 4        int maxLength = 0;
 5        for (int i = 0; i < nums1.length; i++) {
 6            for (int j = 0; j < nums2.length; j++) {
 7
 8                int length = 0;
 9                int p1 = i;
10                int p2 = j;
11
12                while (p1 < nums1.length && p2 < nums2.length && nums1[p1] == nums2[p2]) {
13                    length++;
14                    p1++;
15                    p2++;
16                }
17                maxLength = Math.max(maxLength, length);
18            }
19        }
20        return maxLength;
21    }
22}
23
24// 动规
25class Solution {
26    public int findLength(int[] nums1, int[] nums2) {
27        int res = 0;
28        int[][] dp = new int[nums1.length + 1][nums2.length + 1];
29
30        for (int i = 1; i < nums1.length + 1; i++) {
31            for (int j = 1; j < nums2.length + 1; j++) {
32                if (nums1[i - 1] == nums2[j - 1]) {
33                    dp[i][j] = dp[i - 1][j - 1] + 1;
34                    res = Math.max(res, dp[i][j]); 
35                }
36            }
37        }
38        return res;
39    }
40}

3.复杂度分析

时间复杂度:O(n^2).

空间复杂度:O(n).

相关推荐
复杂网络3 小时前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案
算法
HjhIron19 小时前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩20 小时前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹1 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术1 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望1 天前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法
黄敬峰1 天前
面试必刷:从JS底层包装类到双指针,彻底搞懂字符串与回文算法
算法
地平线开发者2 天前
J6B vio scenario sample
算法
BothSavage2 天前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法