Longest increasing subsequence 看来也是道比较经典的算法题,LeetCode 与 LintCode 都有此题。
有名的算法网站 Rosetta Code 上有此题的N种语言之实现 Longest increasing subsequence
Dynamic Programming
琢磨不出来,上网搜罗
google关键字:
- longest increasing subsequence dynamic programming
- longest increasing subsequence optimal substructure
这是相关资料:
- Longest Increasing Subsequence using Dynamic Programming
http://www.techiedelight.com/longest-increasing-subsequence-using-dynamic-programming/ 最后是按这篇blog的思路写的代码 - Dynamic Programming #1: Longest Increasing Subsequence youtube视频,看了这个明白需要增加一个数组保存对应每个数组slice的递增子序列的长度:length of LIS ends with D[i]
- Longest increasing subsequence
https://wcipeg.com/wiki/Longest_increasing_subsequence - Can I get an explanation for how optimal substructure is used to find the longest increasing subsequence in this powerpoint slide?
https://stackoverflow.com/questions/39074402/can-i-get-an-explanation-for-how-optimal-substructure-is-used-to-find-the-longes - Dynamic Programming – Longest Increasing Subsequence
https://algorithms.tutorialhorizon.com/dynamic-programming-longest-increasing-subsequence/
代码虽然在LeetCode上跑通了,但还是没有完全理解其中的步骤,还是处于知其然而不知其所以然的阶段,好像有点数学归纳法的意味,也有点像递归,动态规划好像也能用来解决兔子序列问题。
java
Efficient algorithms followed Wikipedia
binary search + DP
1 | class Solution { |
Submission Detail
- 54 / 54 test cases passed.
- Runtime: 5 ms, faster than 75.39% of Java online submissions for Longest Increasing Subsequence.
- Memory Usage: 39.2 MB, less than 12.91% of Java online submissions for Longest Increasing Subsequence.
o(n^2)
1 | class Solution { |
Submission Detail
- 24 / 24 test cases passed.
- Your runtime beats 5.56 % of java submissions.
javascript
1 | /** |
Submission Detail
- 24 / 24 test cases passed.
- Your runtime beats 13.56 % of javascript submissions.
此题使用动态规划的算法看来性能不好,时间复杂度为O(n2),维基百科上说最有效的算法为 Patience sorting,能达到 O(n log n)