当前位置:网站首页>2022.05.23(LC_300_最长递增子序列)
2022.05.23(LC_300_最长递增子序列)
2022-06-10 18:16:00 【Leeli9316】
方法:动态规划
①确定状态:dp[i] 为i之前包括i的以nums[i]结尾最长递增子序列长度;
②转移方程:
位置i的最长升序子序列等于j从0到i-1各个位置的最长升序子序列 + 1 的最大值,
所以:if (nums[i] > nums[j]) dp[i] = max(dp[i], dp[j] + 1),
注意这里不是要dp[i] 与 dp[j] + 1进行比较,而是要取dp[j] + 1的最大值;
③初始条件和边界情况:dp[0] = 1,每一个i,对应的dp[i](即最长上升子序列)起始大小至少都是1;
④计算顺序:因为 dp[i] 是0到i-1各个位置的最长升序子序列推导而来,所以从前往后遍历。
class Solution {
public int lengthOfLIS(int[] nums) {
int n = nums.length;
if (n == 0) return 0;
int[] dp = new int[n];
dp[0] = 1;
int ans = 1;
for (int i = 1; i < n; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
ans = Math.max(ans, dp[i]);
}
return ans;
}
}边栏推荐
- Opencv does not rely on any third-party database for face detection
- 2022.05.24(LC_674_最长连续递增序列)
- Use of uiautomator2 automated test tool
- SQL function
- Jsp基于ssm项目实验室管理系统设计与现实.doc
- Chapter IV data type (III)
- [kuangbin] topic 12 basic DP1
- How to correctly understand the real-time nature of Bi?
- Db2 SQL PL的锚点类型和行数据类型
- Three ways generated by stream lambda
猜你喜欢

Introduction to ad18 device library import

Openssl1.1.1 compilation error can't locate win32/console pm in @INC

【 random talk 】 congratulations on getting the title of CSDN expert. Your efforts will eventually pay off

nodejs-基本架构分析-解析引擎目录-插件安装-核心模块

MySQL高级篇第一章(linux下安装MySQL)【上】

阵列信号处理仿真之四——Z变换分析阵列多项式

Pits encountered during the use of ETL (ETL Chinese garbled)

mysql8.0(新特性小结)

Vcsa7u3c installation tutorial

Openssl1.1.1 VS2013-编译教程
随机推荐
WordPress 6.0 “Arturo阿图罗” 发布
Db2 SQL PL的锚点类型和行数据类型
Introduction to ad18 device library import
Adobe Premiere基础-不透明度(蒙版)(十一)
第三章 数据类型(二)
调试的技巧
Mysql (17 déclencheurs)
单纯形法代码求解(含超详细代码注释和整个流程图)
超级简单的课程设计ssm学生管理系统(含源码简单添加、删除、修改、查询操作)
Wireshark learning notes (I) common function cases and skills
2022.05.28(LC_516_最长回文子序列)
Adobe Premiere基础-介绍,配置,快捷键,创建项目,创建序列(一)
lingo12软件下载及lingo语言入门资源
Openssl1.1.1 VS2013-编译教程
5. golang generics and reflection
Vcsa7u3c installation tutorial
直播预告 | 社交新纪元,共探元宇宙社交新体验
Low carbon data center construction ideas and future trends
数据库防火墙的性能和高可用性分析
Rewrite clear Bayesian formula with base ratio