当前位置:网站首页>[dynamic programming] longest increasing subsequence
[dynamic programming] longest increasing subsequence
2022-04-23 07:17:00 【Breeze_】
Give you an array of integers
nums
, Find the length of the longest strictly increasing subsequence .Input :nums = [10,9,2,5,3,7,101,18]
Output :4
explain : The longest increasing subsequence is [2,3,7,101], So the length is 4 .Reference link https://leetcode-cn.com/problems/longest-increasing-subsequence/
Definition : d p [ i ] dp[i] dp[i] Show from 0 To i i i The longest increasing subsequence by far
The boundary conditions : d p [ i ] = 1 , i dp[i]=1,i dp[i]=1,i from 0 0 0 To N − 1 N-1 N−1
State transition equation : d p [ i ] = m a x ( d p [ j ] ) + 1 , i f n u m s [ i ] > n u m s [ j ] dp[i] = max(dp[j])+1~,~if ~~nums[i]~>~nums[j] dp[i]=max(dp[j])+1 , if nums[i] > nums[j]
explain : Every index number bit i i i The longest increasing subsequence up to , Equal to traversing from 0 To i So far the dp Maximum value , At the same time, the value corresponding to the maximum value should be compared with the index number i The value should be small
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
N = len(nums)
dp = [1]*N
ans = 1
for i in range(1,N):
for j in range(i):
if nums[j]<nums[i]:
dp[i] = max(dp[i],dp[j]+1)
ans = max(ans,dp[i])
return ans
Find the largest dp When , Binary search can be used to optimize , bring O(nlogn)
版权声明
本文为[Breeze_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610323460.html
边栏推荐
- [Exynos4412][iTOP4412][Android-K]添加产品选项
- [recommendation of new books in 2021] enterprise application development with C 9 and NET 5
- c语言编写一个猜数字游戏编写
- JVM basics you should know
- 红外传感器控制开关
- Exploration of SendMessage principle of advanced handler
- 杂七杂八的学习
- 【動態規劃】不同路徑2
- 如何对多维矩阵进行标准化(基于numpy)
- 【2021年新书推荐】Effortless App Development with Oracle Visual Builder
猜你喜欢
随机推荐
Binder mechanism principle
[recommendation of new books in 2021] practical IOT hacking
PaddleOCR 图片文字提取
C connection of new world Internet of things cloud platform (simple understanding version)
AVD Pixel_ 2_ API_ 24 is already running. If that is not the case, delete the files at C:\Users\admi
ArcGIS License Server Administrator 无法启动解决方法
【动态规划】不同的二叉搜索树
“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
【动态规划】最长递增子序列
PyTorch中的一些常见数据类型转换方法,与list和np.ndarray的转换方法
MySQL笔记5_操作数据
Itop4412 kernel restarts repeatedly
[2021 book recommendation] practical node red programming
MySQL notes 5_ Operation data
Itop4412 surfaceflinger (4.4.4_r1)
红外传感器控制开关
MySQL笔记2_数据表
Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation
PyTorch训练一个网络的基本流程5步法
Markdown basic grammar notes