当前位置:网站首页>[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)
边栏推荐
- [Blue Bridge Road -- bug free code] DS18B20 temperature reading code analysis
- [set theory] set concept and relationship (set family | set family examples | multiple sets)
- 重绘和回流
- Xrandr modifier la résolution et le taux de rafraîchissement
- RSRS指标择时及大小盘轮动
- Arlo's thinking about himself
- [Apple Photo Album push] IMessage group anchor local push
- In Net 6 project using startup cs
- [graduation season · aggressive technology Er] Confessions of workers
- Causal AI, a new paradigm for industrial upgrading of the next generation of credible AI?
猜你喜欢
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func main() { var a =
JS realizes the animation effect of text and pictures in the visual area
【刷题篇】 找出第 K 小的数对距离
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
redis 持久化原理
JMeter starts from zero (III) -- simple use of regular expressions
leetcode:297. Serialization and deserialization of binary tree
Nodejs Foundation: shallow chat URL and querystring module
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
竞品分析撰写
随机推荐
解决bp中文乱码
The latest activation free version of Omni toolbox
eth入门之简介
拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
Square root of X
Two points -leetcode-540 A single element in an ordered array
[mathematical logic] predicate logic (toe normal form | toe normal form conversion method | basic equivalence of predicate logic | name changing rules | predicate logic reasoning law)
RSRS指标择时及大小盘轮动
540. Single element in ordered array
Basic syntax of class
Feature_selection
[set theory] inclusion exclusion principle (including examples of exclusion principle)
Interaction free shell programming
[set theory] set concept and relationship (true subset | empty set | complete set | power set | number of set elements | power set steps)
Basic MySQL operations
2022 polymerization process examination questions and polymerization process examination skills
Daily question - ugly number
[brush questions] most elements (super water king problem)
[daily question] dichotomy - find a single dog (Bushi)
Basic types of data in TS