当前位置:网站首页>[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
边栏推荐
- 【动态规划】最长递增子序列
- iTOP4412 SurfaceFlinger(4.0.3_r1)
- MySQL5.7插入中文数据,报错:`Incorrect string value: ‘\xB8\xDF\xAE\xF9\x80 at row 1`
- [recommendation of new books in 2021] enterprise application development with C 9 and NET 5
- MySQL5. 7 insert Chinese data and report an error: ` incorrect string value: '\ xb8 \ XDF \ AE \ xf9 \ X80 at row 1`
- Markdown basic grammar notes
- 去掉状态栏
- ./gradlew: Permission denied
- C connection of new world Internet of things cloud platform (simple understanding version)
- 组件化学习(2)Arouter原理学习
猜你喜欢
1.1 PyTorch和神经网络
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
What did you do during the internship
webView因证书问题显示一片空白
Android面试计网面经大全【持续更新中。。。】
[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide
一款png生成webp,gif, apng,同时支持webp,gif, apng转化的工具iSparta
Ffmpeg common commands
Project, how to package
Android interview Online Economic encyclopedia [constantly updating...]
随机推荐
从0开始封装一套项目的网络请求框架
org. xml. sax. SAXParseException; lineNumber: 141; columnNumber: 252; cvc-complex-type. 2.4. a: Found element 'B
【動態規劃】不同路徑2
What did you do during the internship
face_recognition人脸检测
Component learning
Itop4412 HDMI display (4.4.4_r1)
[recommendation of new books in 2021] enterprise application development with C 9 and NET 5
一款png生成webp,gif, apng,同时支持webp,gif, apng转化的工具iSparta
记录webView显示空白的又一坑
常用UI控件简写名
JNI中使用open打开文件是返回-1问题
Exploration of SendMessage principle of advanced handler
MySQL笔记1_数据库
this. getOptions is not a function
PaddleOCR 图片文字提取
ThreadLocal,看我就够了!
Personal blog website construction
Easyui combobox 判断输入项是否存在于下拉列表中
Pytorch模型保存与加载(示例)