当前位置:网站首页>[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
边栏推荐
- [2021 book recommendation] effortless app development with Oracle visual builder
- Android清除应用缓存
- adb shell 常用命令
- 【2021年新书推荐】Practical IoT Hacking
- C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported
- 组件化学习(2)Arouter原理学习
- [2021 book recommendation] artistic intelligence for IOT Cookbook
- SSL/TLS应用示例
- Miscellaneous learning
- Pytorch模型保存与加载(示例)
猜你喜欢
随机推荐
JVM basics you should know
Cause: dx. jar is missing
Itop4412 kernel restarts repeatedly
机器学习笔记 一:学习思路
【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide
SSL/TLS应用示例
1.1 PyTorch和神经网络
[2021 book recommendation] learn winui 3.0
Android面试计网面经大全【持续更新中。。。】
MySQL5. 7 insert Chinese data and report an error: ` incorrect string value: '\ xb8 \ XDF \ AE \ xf9 \ X80 at row 1`
BottomSheetDialogFragment + ViewPager+Fragment+RecyclerView 滑动问题
Component based learning (1) idea and Implementation
树莓派:双色LED灯实验
ProcessBuilder工具类
红外传感器控制开关
一款png生成webp,gif, apng,同时支持webp,gif, apng转化的工具iSparta
iTOP4412内核反复重启
【2021年新书推荐】Kubernetes in Production Best Practices
[dynamic programming] triangle minimum path sum
Fill the network gap









