当前位置:网站首页>最长严格递增子序列
最长严格递增子序列
2022-06-11 18:01:00 【AlbertOS】
引入
给定一个数组arr,返回arr的最长严格递增子序列的长度
子数组是连续的,比如[1,3,5,7,9]的子数组有[1,3],[3,5,7]等等,但是[1,3,7]不是子数组。
示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。
解题方法
使用动态规划的思想,用i遍历数组,然后用j遍历小于i的数据,如果i的数据大于j的数据而且dp[i]的长度小于dp[j]+1的长度,则记录下来;
其实知道解题的方法和递推公式,写起来就简单了,
D ( x ) = { m a x ( d p [ j ] + 1 , d p [ i ] ) , 0 < = j < i 且 a r r [ j ] < a r r [ i ] 1 , 0 < = j < i 且 a r r [ j ] > a r r [ i ] D(x) = \begin{cases} max(dp[j]+1,dp[i] ), & 0<=j<i且arr[j]<arr[i] \\ 1,& 0<=j<i且arr[j]>arr[i] \\ \end{cases} D(x)={ max(dp[j]+1,dp[i]),1,0<=j<i且arr[j]<arr[i]0<=j<i且arr[j]>arr[i]
java代码
class Solution {
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
dp[0] = 1;
int maxans = 1;
for (int i = 1; i < nums.length; 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);
}
}
maxans = Math.max(maxans, dp[i]);
}
return maxans;
}
}
复杂度分析
时间复杂度: O ( n 2 ) O(n^2) O(n2),其中 n 为数组 nums的长度。动态规划的状态数为 n,计算状态dp[i] 时,需要 O(n)的时间遍历 d p [ 0 … i − 1 ] d p [ 0 … i − 1 ] dp[0 \ldots i-1]dp[0…i−1] dp[0…i−1]dp[0…i−1]的所有状态,所以总时间复杂度为 O ( n 2 ) O(n^2) O(n2)
空间复杂度: O ( n ) O(n) O(n),需要额外使用长度为 n 的 dp 数组。
边栏推荐
- Online excel file parsing and conversion to JSON format
- Use egg Js+mongodb simple implementation of curdapi
- SQL报错注入1
- 夜神安装apk,以及bp代理
- Install MariaDB 10.5.7 (tar package installation)
- [collect first and use it sooner or later] 100 Flink high-frequency interview questions series (I)
- Various poses for text modification using sed
- async导致函数结果出乎意料,改变原来代码的意图;await is only valid in async functions and the top level bodies of modules
- [MapReduce] a complete Mr program case teaches you how to package and run with idea
- MMA-Self-defining function
猜你喜欢

How to learn and self-study

Say no to credit card fraud! 100 lines of code to realize simplified real-time fraud detection

jsfinder,wafw00f安装,nmap配置(缺少msvcr120.dll文件)
![[FAQs for novices on the road] about project management](/img/14/68f5e4cead5573fc932350d8d9b06e.png)
[FAQs for novices on the road] about project management
![Spring 2021 daily question [week6 not finished]](/img/32/74cff6f057ef9a7941fd6a41dc6635.jpg)
Spring 2021 daily question [week6 not finished]

SISO Decoder for SPC (补充章节1)
![Winter vacation daily question (improvement group) [end of week 4]](/img/67/89b5164712d8c4eb319b9266dd4b91.jpg)
Winter vacation daily question (improvement group) [end of week 4]

Tle6288r is a 6-channel (150 MOhm) intelligent multi-channel switch using intelligent power technology - keshijin mall

vulhub

Retrofit source code analysis
随机推荐
[Golang]力扣Leetcode - 292. Nim 游戏(数学)
Spring 2021 daily question [week3 not finished]
ACL 2022: is it no longer difficult to evaluate word polysemy? A new benchmark "dibimt"
Reading summary of nacos2.x source code
jsfinder,wafw00f安装,nmap配置(缺少msvcr120.dll文件)
MySQL/Redis 常见面试题汇总
[collect first and use it sooner or later] 100 Flink high-frequency interview questions series (II)
关于keil中,while循环条件不成立却无法跳出的问题
Reading summary of nacos2.x source code
[C语言]对一个数组的元素排序后平移元素
HashSet集合
GB gb28181 protocol video platform easygbs adds or deletes offline channels
任意用户密码重置的10种方式
"College entrance examination" volume sent to the big model: 442 people put forward 204 tasks to the big model, led by Google
SISO decoder for a general (n,n-1) SPC code(補充章節3)
密码学概述
【先收藏,早晚用得到】100个Flink高频面试题系列(三)
Hello go (XIV). Go language common standard library IV
Intelligent overall legend, legend of wiring, security, radio conference, television, building, fire protection and electrical diagram [transferred from wechat official account weak current classroom]
The tle6389 step-down DC-DC switch controller has high efficiency in the whole load range of 1mA to 2.5A - keshijin mall