当前位置:网站首页>8.< tag-动态规划和LCS问题>lt.300. 最长递增子序列 + lt.674. 最长连续递增序列
8.< tag-动态规划和LCS问题>lt.300. 最长递增子序列 + lt.674. 最长连续递增序列
2022-07-23 03:54:00 【菜菜的大数据开发之路】
lt.300. 最长递增子序列
[案例需求]

[思路分析]
有题: dp[i] 是可以用dp[j] (j < i) 推导出来的;
- dp[i] 的定义
dp[i] 在 i之间包括i(即长度i + 1)的以nums[i] 结尾的最长上升子序列的长度.
- 状态转移方程
位置 i的最长升序子序列, 可以从更短的 dp[j] 中的最长上升子序列中得到, 如果nums[i] 在这个上升子序列中也会上升, 那么 dp[i] = dp[j] + 1, 如果不符合, 那dp[i] = dp[j]
综上来看, 状态转移方程为:
if(nums[i] > nums[j]) dp[i] = Math.max(dp[j], dp[j] + 1);
注意这里不是要dp[i] 与 dp[j] + 1进行比较,而是我们要取dp[j] + 1, dp[j] 中的最长连续上升序列的长度。
- dp[i]的初始化
每一个i,对应的dp[i](即最长上升子序列)起始大小至少都是1.
- 确定遍历顺序
dp[i] 是有0到i-1各个位置的最长升序子序列 推导而来,那么遍历i一定是从前向后遍历。
j其实就是0到i-1,遍历i的循环在外层,遍历j则在内层,代码如下:
for (int i = 1; i < nums.size(); i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) dp[i] = max(dp[i], dp[j] + 1);
}
if (dp[i] > result) result = dp[i]; // 取长的子序列
}
- 举例推导dp数组
[代码实现]
class Solution {
public int lengthOfLIS(int[] nums) {
//1. 确定dp[i], dp[i] 由之前的dp[j]推出来的.
// dp[j] 表示 nums[j]前面的数的最长递增子序列
int len = nums.length;
int[] dp = new int[len];
//2. 递推公式
// 是dp[j]的递增, 不是dp[j]的递增
//dp[i] = Math.max(dp[j] + 1, dp[j]);
//3. 初始化
// dp[0] = 1.
for(int i = 0; i < len; i++){
dp[i] = 1;
}
//4. 确定遍历顺序, 正序遍历
for(int i = 1; i < len; i++){
for(int j = 0; j < i; j++){
if(nums[i] > nums[j]){
dp[i] = Math.max(dp[j] + 1, dp[i]);
}
}
}
//取出最大的dp[i]
int res = 0;
for(int x : dp){
res = Math.max(res, x);
}
return res;
}
}
lt.674. 最长连续递增序列
[案例需求]

[思路分析一, 贪心]
[代码实现]

思路分析二, 动态规划
class Solution {
/** * 1.dp[i] 代表当前下标最大连续值 * 2.递推公式 if(nums[i+1]>nums[i]) dp[i+1] = dp[i]+1 * 3.初始化 都为1 * 4.遍历方向,从其那往后 * 5.结果推导 。。。。 * @param nums * @return */
public static int findLengthOfLCIS(int[] nums) {
int[] dp = new int[nums.length];
for (int i = 0; i < dp.length; i++) {
dp[i] = 1;
}
int res = 1;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i + 1] > nums[i]) {
dp[i + 1] = dp[i] + 1;
}
res = res > dp[i + 1] ? res : dp[i + 1];
System.out.println(Arrays.toString(Arrays.copyOf(dp, i + 1)));
}
return res;
}
}


边栏推荐
- 【汇总篇】
- Moment get week, month, quarter, year
- [MySQL] cursor
- 【代码案例】网页版表白墙 & 待办事项 (包含完整源码)
- Use reflection to modify the member variable whose modifier is final
- Data middle office, Bi business interview (III): how to choose the right interviewees
- Use and implementation of enumeration classes
- [vscode] the current working directory is not the current folder /pathlib print CWD path error
- 金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(八))
- LeetCode每日一题(1946. Largest Number After Mutating Substring)
猜你喜欢

1. Assignment statement

【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(七)

世界正在被开源软件吞食

These four key technologies are necessary to realize the unified management of urban governance through one network

Three goals and eight tasks of intelligent construction pilot city notice

RichView TextBox Items 文本框

C language file operation

ssm框架外卖订餐系统

Anaconda source change and opencv installation

What is per title encoding?
随机推荐
Reverse theoretical knowledge 1
你离个人信息泄漏的安全距离,或许一台笔记本电脑就可以决定!
有关字符串的题目总结
UnityC#实现中文汉字转拼音-使用微软CHSPinYinConv库
Ten year structure five year Life-05 first business trip
Android development learning diary - content provider (cross application database modification)
Redis事务-秒杀案例模拟实现详细过程
Illustration and text demonstrate the movable range of the applet movable view
【汇总篇】
[learning notes] agc022
Normal form and anti normal form
金仓数据库 KingbaseES SQL 语言参考手册 (4. 伪列)
How to improve browsing security? Teach you to set up a secure browser trust site
EasyCVR新版本(v2.5.0)目录分级功能如何使用?
C language -- several classic exercises of C language
How switch statements work
mysql三表查询问题
Sonar中如何删除一个项目
Sum of three numbers: (sort + double pointer + pruning)
Undo log日志详解
