当前位置:网站首页>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;
}
}边栏推荐
猜你喜欢

Three ways generated by stream lambda

2022.05.29(LC_6078_重排字符形成目标字符串)

锐捷x32pro刷openwrt开启无线160MHz

What are the current mainstream all-optical technology solutions- Part II

Adobe Premiere foundation - time remapping (10)

openSSL1.1.1编译错误 Can‘t locate Win32/Console.pm in @INC

Live broadcast preview | deconstruct OLAP! The new multidimensional analysis architecture paradigm is fully open! Apache Doris will bring five big issues!

Live broadcast preview | a new era of social interaction, exploring new social experiences in the universe

Introduction to ad18 device library import

Pits encountered during the use of ETL (ETL Chinese garbled)
随机推荐
Openssl1.1.1 VS2013-编译教程
Detailed explanation of Lora module wireless transceiver communication technology
基于谱加权的波束方向图分析
[Code] neural symbol generation machine
【 random talk 】 congratulations on getting the title of CSDN expert. Your efforts will eventually pay off
lingo12软件下载及lingo语言入门资源
Introduction to DB2 SQL pl
Enterprise data quality management: how to evaluate data quality?
Adobe Premiere basic special effects (card point and transition) (IV)
数据治理经典6大痛点?这本书教你解决
TestNG的HelloWorld例子以及如何在命令行下运行
Libcurl 7.61.0 vs2013 compilation tutorial
直播预告 | 社交新纪元,共探元宇宙社交新体验
c(指针02)
【Vulnhub靶场】JANGOW: 1.0.1
WordPress 6.0 "Arturo Arturo" release
Nodejs basic architecture analysis parsing engine directory plug-in installation core module
SPSS入门笔记记录
Vcsa7u3c installation tutorial
基于ssm在线订餐系统设计与实现.rar(项目源码)