题目描述
Given an unsorted array of integers, find the length of longest increasing subsequence.
Example:
Input: [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
Note:
There may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
我的解法
解题思路
用动态规划,状态转移方程应该为:
如果nums[i]>nums[j] (i>j)
dp[i] = max(dp[i], dp[j]+1)
最后的解就是dp数组中的最大值。
实现代码
1 | class Solution { |
Runtime: 76 ms, faster than 8.02% of C++ online submissions for Longest Increasing Subsequence.
Memory Usage: 6.5 MB, less than 100.00% of C++ online submissions for Longest Increasing Subsequence.
673. Number of Longest Increasing-Subsequence
基于300题的进阶题目,要求求出最长的递增子串并且求出数量。看了答案才做出来。除了dp数组以外,还需要维护一个cnt数组,来记录当前长度子串的数量。
1 | class Solution { |