最长递增子子序列,这是动态规划里的经典问题了。
python代码如下
def get_max_increase_sequence1(a): # dp[i] = max{dp[j] | 0<=j<i,a[i]<a[j] } + 1 # O(n**2) num=len(a) dp=[0]*num for i in xrange(num): for j in xrange(i): if a[j]<a[i] and dp[j]>dp[i]: # a[j]<a[i] means that dp[i] should base on dp[j] # update dp[j] and select bigger one dp[i]=dp[j] dp[i]+=1 return dp a=[1,2,3,4,0,1] print get_max_increase_sequence1(a)
get_max_increase_sequence.py
听说还有优化方法,下次再写吧。
时间: 2024-10-12 00:05:14