Longest Increasing Subsequence Review
2016-04-30 20:42
363 查看
LetA[1...n]beasequenceofdistinctintegers.Anincreasingsubsequence(IS)ofAisasubsequenceA[i1],A[i2],...,A[ik],withi1<i2<...<ik,suchthat,foris<it,wehaveA[is]<A[it].Alongestincreasingsubsequence(LIS)ofAisanincreasingsubsequenceofmaximumlength.
Problem:FindthelengthofLISofA
Westartwithasolutionusingdynamicprogramming.
Thenwehavethefollowingstate-transitionequations:
ItisobviousthattherunningtimeofthisalgorithmisO(n^2).
Canwedobetter?Definitely!
LetB[i]bethelastelementofaminimumLISoflengthi.AminimumLISoflengthiherereferstoaLISwhichhassmallestendingelementamongallLISesoflengthi.
WehaveB[i]<B[i+1]forall1<=i<n.(proofbycontradiction)
Usingthisalgorithm,wecanfindthelengthofLISofAinrunningtimeofO(nlgn).
WhatifAhasduplicateelements?
Problem:FindthelengthofLISofA
Westartwithasolutionusingdynamicprogramming.
letF[i]bethelengthofLISofA[1...i]. letG[i]bethemaximumlengthofISendingwithA[i].
Thenwehavethefollowingstate-transitionequations:
G[i]=max{G[j]+1}forall1<=j<iwithA[i]>A[j] or1ifsuchajdoesnotexist F[i]=max{G[j]}forall1<=j<=i
ItisobviousthattherunningtimeofthisalgorithmisO(n^2).
Canwedobetter?Definitely!
LetB[i]bethelastelementofaminimumLISoflengthi.AminimumLISoflengthiherereferstoaLISwhichhassmallestendingelementamongallLISesoflengthi.
WehaveB[i]<B[i+1]forall1<=i<n.(proofbycontradiction)
//initializeB forifrom1ton B[i]=nil //calculateB forifrom1ton ifthere'ssuchajwith1<=j<i-1andB[j]<A[i]<B[j+1] B[j+1]=A[i] elseifA[i]<B[1] B[1]=A[i] elseifA[i]>B[i-1] B[i]=A[i] //findtheanswer len=1 forifromndownto1 ifB[i]isnotnil len=i break //thenlenholdsthelengthofLISofA.
Usingthisalgorithm,wecanfindthelengthofLISofAinrunningtimeofO(nlgn).
Discussion
WhatifAhasduplicateelements?
Thetwoalgorithmsabovearealsocorrectforsolvingthiscase.
Whatifwewanttofindthelengthoflongestnon-decreasingsubsequenceofasequencewhichmayhaveduplicateelements?
Itistrivaltomodifythefirstalgorithmtosolvethisproblem,weonlyneedtochangetransitionequationofG[i]to:
G[i]=max{G[j]+1}forall1<=j<iwithA[i]>=A[j] or1ifsuchajdoesnotexist
Forthesecondalgorithm,wehavepropertyB[i]<=B[i+1]insteadofB[i]<B[i+1]inthiscase.AndthecalculatingprocessofBshouldbemodified.
//calculateB forifrom1ton ifthere'ssuchajwith1<=j<i-1andB[j]<=A[i]<B[j+1] B[j+1]=A[i] elseifA[i]<B[1] B[1]=A[i] elseifA[i]>=B[i-1] B[i]=A[i]
相关文章推荐
- UIScrollView 的代理方法简单注解
- android四种更新UI进程
- 54.java编程思想——创建窗口和程序片 逻辑与UI分离
- 提高UI流畅度
- Codeforces #349 div1 A. Reberland Linguistics 模拟 递推
- LIGHTOJ1048_Conquering Keokradong
- requires the IDE to be running with java 1.8 or later 解决方法
- 225. Implement Stack using Queues
- 232. Implement Queue using Stacks
- UILabel(标签栏)、UIAlertView(警告栏)、UIActionSheet(上拉菜单)
- poj 2778 DNA Sequence(AC自动机 + 矩阵快速幂)
- C#中GUID的生成格式(Guid.ToString方法 )
- 提示 延时自动关闭
- UIView
- Compile、Make和Build的区别
- Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set
- kernel_require.rb:55:in `require': cannot load such file
- 矩阵快速幂算法+列题(HDU 5667 Sequence)
- The server timezone value 'UTC' is unrecognized or represents more than one timezone
- LeetCode[Stack]----Min Stack&Stack&Queue