当前位置:网站首页>Longest strictly increasing subsequence
Longest strictly increasing subsequence
2022-06-11 19:07:00 【AlbertOS】
introduce
Given an array arr, return arr The length of the longest strictly increasing subsequence of
Subarray is continuous , such as [1,3,5,7,9] The subarrays of are [1,3],[3,5,7] wait , however [1,3,7] It's not a subarray .
Example : Input : [10,9,2,5,3,7,101,18] Output : 4 explain : The longest ascending subsequence is [2,3,7,101], Its length is 4.
How to solve the problem
Using the idea of dynamic programming , use i Traversal array , And then use j Ergodic less than i The data of , If i The data of is greater than j And dp[i] The length of is less than dp[j]+1 The length of , Then write it down ;
In fact, I know how to solve problems and the recurrence formula , It's easy to write ,
D ( x ) = { m a x ( d p [ j ] + 1 , d p [ i ] ) , 0 < = j < i And a r r [ j ] < a r r [ i ] 1 , 0 < = j < i And a r r [ j ] > a r r [ i ] D(x) = \begin{cases} max(dp[j]+1,dp[i] ), & 0<=j<i And arr[j]<arr[i] \\ 1,& 0<=j<i And arr[j]>arr[i] \\ \end{cases} D(x)={ max(dp[j]+1,dp[i]),1,0<=j<i And arr[j]<arr[i]0<=j<i And arr[j]>arr[i]
java Code
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;
}
}
Complexity analysis
Time complexity : O ( n 2 ) O(n^2) O(n2), among n Is an array nums The length of . The number of states of dynamic programming is n, Calculation state dp[i] when , need O(n) Time traversal 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] All states , So the total time complexity is O ( n 2 ) O(n^2) O(n2)
Spatial complexity : O ( n ) O(n) O(n), Additional length required is n Of dp Array .
边栏推荐
- 在 SAP Kyma 上部署一个 Go MSSQL API Endpoint
- 7-3 combinatorial problems (*)
- The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!
- [video denoising] video denoising based on salt with matlab code
- [Multisim Simulation] generate square wave and triangular wave generators by operational amplifier
- What is the workflow of dry goods MapReduce?
- 【图像去噪】基于绝对差分中值滤波、加权中值滤波法、改进加权中值滤波实现脉冲噪声图像去噪附matlab代码
- leetcode:剑指 Offer 59 - II. 队列的最大值[deque + sortedlist]
- 【Multisim仿真】利用运算放大器产生锯齿波
- Flask CKEditor 富文本编译器实现文章的图片上传以及回显,解决路径出错的问题
猜你喜欢

Programmers have changed dramatically in 10 years. Everything has changed, but it seems that nothing has changed

【Multisim仿真】利用运算放大器产生方波、三角波发生器

cf:E. Price Maximization【排序 + 取mod + 双指针+ 配对】

Understand how to get started with machine learning to quantify transactions?

Flask CKEditor 富文本编译器实现文章的图片上传以及回显,解决路径出错的问题

疫情下远程办公沟通心得|社区征文

leetcode:剑指 Offer 56 - II. 数组中数字出现的次数 II【简单排序】

【Multisim仿真】利用运算放大器产生锯齿波
![[Multisim Simulation] using operational amplifier to generate sawtooth wave](/img/98/27086746dc552ada25fd36a82cb52b.png)
[Multisim Simulation] using operational amplifier to generate sawtooth wave

cf:D. Black and White Stripe【连续k个中最少的个数 + 滑动窗口】
随机推荐
Internet_ Business Analysis Overview
Cf:c. restoring the duration of tasks
MOS transistor 24n50 parameters of asemi, 24n50 package, 24n50 size
On the translation of rich text storage database format
leetcode:剑指 Offer 59 - II. 队列的最大值[deque + sortedlist]
Crop disease detection using image processing technology and convolutional neural network (CNN)
视觉SLAM十四讲笔记-10-2
[signal denoising] speech adaptive denoising based on nonlinear filter with matlab code
用户信息管理的功能开发
Gmail:如何撤回发出的邮件?
关于我的 “二进制部署 kubernetes 集群” 的体验
基于华为云图像识别标签实战
kubernetes 二进制安装(v1.20.15)(八)部署 网络插件
Cf:d. black and white stripe
Performance of MOS transistor 25n120 of asemi in different application scenarios
About my experience of "binary deployment kubernetes cluster"
SQL注入漏洞学习之一:phpstudy集成环境搭建DVWA靶场
High concurrency architecture design
实现可以继续上局
2022 coming of age ceremony, to every college entrance examination student