当前位置:网站首页>【动态规划】最长递增子序列
【动态规划】最长递增子序列
2022-04-23 06:11:00 【小风_】
给你一个整数数组
nums
,找到其中最长严格递增子序列的长度。输入:nums = [10,9,2,5,3,7,101,18]
输出:4
解释:最长递增子序列是 [2,3,7,101],因此长度为 4 。参考链接https://leetcode-cn.com/problems/longest-increasing-subsequence/
定义: d p [ i ] dp[i] dp[i]示从0到 i i i为止最长的递增子序列
边界条件: d p [ i ] = 1 , i dp[i]=1,i dp[i]=1,i从 0 0 0到 N − 1 N-1 N−1
状态转移方程: 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]
说明:每一个索引号位 i i i的为止的最长递增子序列,等于遍历从0到i为止的dp值的最大,同时该最大值对应的数值要比索引号i数值要小
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
在查找最大的dp的时候,可以利用二分查找优化,使得O(nlogn)
版权声明
本文为[小风_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_33952811/article/details/119106111
边栏推荐
- [多屏互动] 实现双多屏异显二:startActivity方式
- Android清除应用缓存
- Component learning
- 常用UI控件简写名
- MySQL5.7插入中文数据,报错:`Incorrect string value: ‘\xB8\xDF\xAE\xF9\x80 at row 1`
- 接口幂等性问题
- 电脑关机程序
- AVD Pixel_2_API_24 is already running.If that is not the case, delete the files at C:\Users\admi
- Component based learning (3) path and group annotations in arouter
- 读书小记——Activity
猜你喜欢
随机推荐
c语言编写一个猜数字游戏编写
项目,怎么打包
【2021年新书推荐】Practical IoT Hacking
Using stack to realize queue out and in
Thanos.sh灭霸脚本,轻松随机删除系统一半的文件
Encapsulate a set of project network request framework from 0
Android清除应用缓存
从0开始封装一套项目的网络请求框架
Ffmpeg common commands
[recommendation of new books in 2021] practical IOT hacking
Cause: dx.jar is missing
MySQL notes 2_ data sheet
MySQL笔记1_数据库
Itop4412 HDMI display (4.4.4_r1)
Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation
记录webView显示空白的又一坑
ThreadLocal,看我就够了!
Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
实习做了啥
组件化学习