Topic:
Given an unsorted array of integers, find the length of longest increasing subsequence.
For example,
Given [10, 9, 2, 5, 3, 7, 101, 18]
,
The longest increasing subsequence [2, 3, 7, 101]
is and therefore the length is 4
. Note that there could be more than one LIS combination, it's only necessary for you and return the length.
Your algorithm should run in O (n2) complexity.
Follow up:could you improve it to O (n log n) time complexity?
Links: http://leetcode.com/problems/longest-increasing-subsequence/
Exercises
The longest increment subsequence of the array. Classic DP problem, in many universities to talk about DP tutorial, will appear this problem and longest Common subsequence. Here actually also has O (Nlogn) method, for example patience sorting one kind of, two brush again study. Now let's look at DP. This problem can be decomposed into recursive problem at the beginning, and the iterative solution with memorization can be obtained by step-by-step optimization. Initialize Dp[i] = 1, which is an ascending sequence of an element. Assuming that the LIS in Subarray in the end of I-1 is dp[i-1], then we require lis,dp[i in Subarray at the end of I, to compare this new element with all the previous elements, while gradually comparing DP[J] + 1 and dp[i], If a longer sequence is found, we update dp[i] = Dp[j] + 1 and continue to increase j for comparison. When all the elements before I are fully facilitated, we get the LIS in the Subarray that is currently ending with I, which is dp[i].
Time Complexity-o (n2), Space Complexity-o (n2).
Public classSolution { Public intLengthoflis (int[] nums) { if(Nums = =NULL|| Nums.length = = 0) { return0; } intLen = nums.length, max = 0; int[] DP =New int[Len]; for(inti = 0; i < Len; i++) {Dp[i]= 1; for(intj = 0; J < I; J + +) { if(Nums[i] > Nums[j] && dp[j] + 1 >Dp[i]) {Dp[i]= Dp[j] + 1; }} Max=Math.max (Max, dp[i]); } returnMax; }}
Off Topic:
#300题! It's another milestone. Although many of the previous problems have been forgotten, but I believe that the second brush will be good to consolidate and re-study. Group in a brush problem of small partners, many have already got Amazon's offer, I also want to work hard just. This week on vacation at home, Wednesday to continue to repair the house, hope everything goes well. At the same time I hope that this week will be able to leetcode the first time to complete, and then early learning new knowledge, such as multi-threading, design patterns, and some system design and so on.
Reference:
Https://leetcode.com/discuss/67609/short-java-solution-using-dp-o-n-log-n
Https://leetcode.com/discuss/67554/9-lines-c-code-with-o-nlogn-complexity
Https://leetcode.com/discuss/67533/c-typical-dp-2-solution-and-nlogn-solution-from-geekforgeek
Https://leetcode.com/discuss/67565/simple-java-o-nlogn-solution
Https://leetcode.com/discuss/71129/space-log-time-short-solution-without-additional-memory-java
Https://leetcode.com/discuss/67687/c-o-nlogn-solution-with-explainations-4ms
Https://leetcode.com/discuss/69309/c-o-nlogn-with-explanation-and-references
Https://leetcode.com/discuss/67572/o-nlogn-and-o-n-2-java-solutions
Https://leetcode.com/discuss/67689/4ms-o-nlogn-non-recursive-easy-to-understand-java-solution
Https://leetcode.com/discuss/67553/share-java-dp-solution
Https://leetcode.com/discuss/72127/easy-to-understand-solution-using-dp-with-video-explanation
Https://leetcode.com/discuss/67806/another-o-n-log-n-python
http://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/
Http://www.cs.cornell.edu/~wdtseng/icpc/notes/dp2.pdf
Https://courses.engr.illinois.edu/cs473/sp2011/lectures/08_notes.pdf
Http://www.cs.toronto.edu/~vassos/teaching/c73/handouts/lis.pdf
Http://www.cs.mun.ca/~kol/courses/2711-w08/dynprog-2711.pdf
Https://courses.cs.washington.edu/courses/cse417/02wi/slides/06dp-lis.pdf
Https://www.cs.princeton.edu/courses/archive/spring13/cos423/lectures/LongestIncreasingSubsequence.pdf
Https://en.wikipedia.org/wiki/Patience_sorting
Https://en.wikipedia.org/wiki/Longest_increasing_subsequence
300.Longest increasing subsequence