当前位置:网站首页>[dynamic programming] subsequence problem
[dynamic programming] subsequence problem
2022-07-03 04:16:00 【Messy hair Aoba】
Subsequence problem is also a problem with a lot of problems .「 Random code recording 」 Take you through DP Subsequence problem !300. The longest increasing subsequence - The longest increasing subsequence - Power button (LeetCode) (leetcode-cn.com)
300. The longest increasing subsequence - Power button (LeetCode) (leetcode-cn.com)
In turn, to solve :
1.【300】 The longest increasing subsequence
300. The longest increasing subsequence - Power button (LeetCode) (leetcode-cn.com)
1.1. Dynamic programming
Honest double-layer circulation method .
Time complexity :O(n^2)
Spatial complexity :O(n)
The code is as follows :
class Solution(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dp=[1 for _ in nums]
for i in range(len(nums)):
for j in range(i):
if nums[i]>nums[j]:
dp[i]=max(dp[i],dp[j]+1)
return max(dp)
1.2. The law of greed + Dichotomy search
Create an empty array d Store the found incremental subsequence , Traversal array nums, If nums[i]>d[-1], be append; otherwise , Binary search will nums[i]【 Insert and replace 】 To the increasing subsequence , That is to find the closest nums[i]<=d[j],d[j]=nums[i],d The length is constant . The result of doing so ,d The final result in the array is not necessarily the real largest subsequence of the sequence .
example :
nums:[1,3,6,7,9,4,10,5,6]
d:[1,3,4,5,6,10]
The actual longest subsequence :[1,3,6,7,9,10]
d The record of searching for the largest subsequence is preserved in the array , If you change the starting point of the array , Then the new sequence must be longer than the previous sequence, then the array length will change , Because the final solution is length , So even so len(d) Still the right answer .
example :
nums:[4,5,6,7,8,1,2]
d:[1,2,6,7,8]
The actual longest subsequence :[4,5,6,7,8]
nums:[7,8,1,2,3,4,5,6]
d:[1,2,3,4,5,6]
The actual longest subsequence :[1,2,3,4,5,6]
Time complexity :O(nlogn)
Spatial complexity :O(m)
The implementation code is as follows :
class Solution(object):
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
d=[]
for n in nums:
if not d or n>d[-1]:
d.append(n)
else:
left,right=0,len(d)-1
loc=right
while left<=right:
mid=(left+right)//2
if n<d[mid]:
loc=mid
right=mid-1
elif n>d[mid]:
left=mid+1
else:
loc=mid
break
d[loc]=n
return len(d)
After finishing this problem, I found , If you use greed + Two points search , Then the detail to pay attention to is binary search , Binary search advice : Don't omit any case .
Tortured , So post the results .

Detailed explanation of the boundary of binary search : Explain the binary search algorithm in detail - murphy_gb - Blog Garden (cnblogs.com)
There's another way 【 Dynamic programming + Two points search 】: Longest ascending subsequence - The longest increasing subsequence - Power button (LeetCode) (leetcode-cn.com)
边栏推荐
- RSRS指标择时及大小盘轮动
- PostgreSQL database high availability Patroni source code learning - etcd class
- 【刷题篇】多数元素(超级水王问题)
- When writing a web project, SmartUpload is used for file upload and new string () is used for transcoding, but in the database, there will still be random codes similar to poker
- [fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments
- Redraw and reflow
- [NLP]—sparse neural network最新工作简述
- Classes in TS
- [daily question] dichotomy - find a single dog (Bushi)
- IPv6 foundation construction experiment
猜你喜欢
![[brush questions] most elements (super water king problem)](/img/79/13a715b74bc18a4a62113de76a65f6.png)
[brush questions] most elements (super water king problem)

Database management tool, querious direct download

2022 Shandong Province safety officer C certificate examination questions and Shandong Province safety officer C certificate simulation examination question bank

Interaction free shell programming

Feature_selection

Bisher - based on SSM pet adoption center

Supervised pre training! Another exploration of text generation!

2022 mobile crane driver examination registration and mobile crane driver operation examination question bank

leetcode:297. Serialization and deserialization of binary tree

有监督预训练!文本生成又一探索!
随机推荐
毕设-基于SSM宠物领养中心
[Apple Push] IMessage group sending condition document (push certificate) development tool pushnotification
2022 electrician (Advanced) examination papers and electrician (Advanced) examination skills
China Mobile Internet of things oneos and onenet were selected in the list of 2021 Internet of things demonstration projects
Two points -leetcode-540 A single element in an ordered array
Arlo's thinking about himself
When writing a web project, SmartUpload is used for file upload and new string () is used for transcoding, but in the database, there will still be random codes similar to poker
Write it down once Net travel management background CPU Explosion Analysis
Sklearn data preprocessing
ZIP文件的导出
国产PC系统完成闭环,替代美国软硬件体系的时刻已经到来
[Yu Yue education] reference materials of political communication science of Communication University of China
[mathematical logic] predicate logic (judge whether the first-order predicate logic formula is true or false | explain | example | predicate logic formula type | forever true | forever false | satisfi
How to connect WiFi with raspberry pie
"Designer universe" argument: Data Optimization in the design field is finally reflected in cost, safety and health | chinabrand.com org
x Problem B
[brush questions] find the number pair distance with the smallest K
xrandr修改分辨率與刷新率
Nat. Comm. | 使用Tensor-cell2cell对细胞通讯进行环境感知去卷积
[NLP]—sparse neural network最新工作简述