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

LeetCode:300.最长递增子序列

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

1.思路

dp[i]的状态表示以nums[i]为结尾的最长递增子序列的个数。

dp[i]有很多个,选择其中最大的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.思路

动规dp[i][j]定义很关键,当前状态需要前一个状态推导出来。

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).

相关推荐
椰萝Yerosius4 小时前
[题解]2024CCPC郑州站——Z-order Curve
c++·算法
小曹要微笑4 小时前
STM32F7 时钟树简讲(快速入门)
c语言·stm32·单片机·嵌入式硬件·算法
南山安4 小时前
栈(Stack):从“弹夹”到算法面试题的进阶之路
javascript·算法·面试
2301_764441335 小时前
Python构建输入法应用
开发语言·python·算法
AI科技星5 小时前
为什么变化的电磁场才产生引力场?—— 统一场论揭示的时空动力学本质
数据结构·人工智能·经验分享·算法·计算机视觉
TheLegendMe6 小时前
贪心+线程安全单例
算法·哈希算法
豐儀麟阁贵7 小时前
8.5在方法中抛出异常
java·开发语言·前端·算法
胖咕噜的稞达鸭7 小时前
算法入门:滑动窗口--->找到字符串中所有的字母异位词,串联所有的子串,最小覆盖子串
数据库·redis·算法
小青龙emmm7 小时前
2025级C语言第二次周测(国教专用)题解
c语言·开发语言·算法
WolfGang0073218 小时前
代码随想录算法训练营Day28 | 509.斐波那契数列、70.爬楼梯、746.使用最小花费爬楼梯
算法