LintCode -- longest-increasing-subsequence(最长上升子序列)
原题链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-subsequence/
给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度。
给出[5,4,1,2,3],这个LIS是[1,2,3],返回 3
给出[4,2,4,5,3,7],这个LIS是[4,4,5,7],返回 4
要求时间复杂度为O(n^2) 或者O(nlogn)
最长上升子序列的定义:
dp [ i ] 表示长度为 i 的上升子序列的最后一个元素的最小值。容易证明,dp 是一个递增数组。
扫描给定 nums 数组,在 dp 数组中查找比 nums[ j ] 小的数中下标最大值 x ,更新 dp,dp[ x+1 ] = min( dp[ x+1 ], nums[ i ] )。
**** 时间复杂度 O(nlgn), 空间复杂度 O(n)****
代码【O( nlgn )】(C++、Java):
class Solution { public: /** * @param nums: The integer array * @return: The length of LIS (longest increasing subsequence) */ int min(int a, int b){ if (a < b) return a; else return b; } int search(vector<int> dp, int i, int j, int k){ if (i == j) return i; else{ int p = (i+j) / 2; if(dp[p] > k) return search(dp, i, p-1, k); else if ((dp[p] < k && dp[p+1] <= k) || (dp[p] == k && dp[p+1] == k)) return search(dp, p+1, j, k); else return p; } } int longestIncreasingSubsequence(vector<int> nums) { // write your code here O(nlgn) int n = nums.size(); if (n <= 1) return n; vector<int> dp; int res = 1, x; dp.push_back(0); dp.push_back(1e9); for (int i = 0; i < n; i++){ dp.push_back(1e9); x = search(dp, 1, res, nums[i]); if (x == 1 && nums[i] < dp[x]) dp[x] = nums[i]; else{ if (x == res) res += 1; dp[x+1] = min(dp[x+1], nums[i]); } } return res; } };
public class Solution { /** * @param nums: The integer array * @return: The length of LIS (longest increasing subsequence) */ int min(int a, int b){ if (a < b) return a; else return b; } int search(int [] dp, int i, int j, int k){ if (i == j) return i; else{ int p = (i+j) / 2; if(dp[p] > k) return search(dp, i, p-1, k); else if ((dp[p] < k && dp[p+1] <= k) || (dp[p] == k && dp[p+1] == k)) return search(dp, p+1, j, k); else return p; } } public int longestIncreasingSubsequence(int[] nums) { // write your code here O(nlgn) int n = nums.length; if (n <= 1) return n; int [] dp = new int [n+1+10]; int res = 1, x; dp[0] = 0; dp[1] = 1000000; for (int i = 0; i < n; i++){ dp[i+2] = 1000000; x = search(dp, 1, res, nums[i]); if (x == 1 && nums[i] < dp[x]) dp[x] = nums[i]; else{ if (x == res) res += 1; dp[x+1] = min(dp[x+1], nums[i]); } } return res; } }
class Solution { public: /** * @param nums: The integer array * @return: The length of LIS (longest increasing subsequence) */ int max(int a, int b){ if (a > b) return a; else return b; } int longestIncreasingSubsequence(vector<int> nums) { // write your code here O(n^2) int n = nums.size(); if (n <= 1) return n; vector<int> dp; int res = 1; dp.push_back(1); for (int i = 1; i < n; i++){ dp.push_back(1); for (int j = 0; j < i; j++) if (nums[j] <= nums[i]) dp[i] = max(dp[i], dp[j] + 1); if (dp[i] > res) res = dp[i]; } return res; } };
public class Solution { /** * @param nums: The integer array * @return: The length of LIS (longest increasing subsequence) */ int max(int a, int b){ if (a > b) return a; else return b; } public int longestIncreasingSubsequence(int[] nums) { // write your code here O(n^2) int n = nums.length; if (n <= 1) return n; int [] dp = new int [n+1]; int res = 1; dp[0] = 1; for (int i = 1; i < n; i++){ dp[i] = 1; for (int j = 0; j < i; j++) if (nums[j] <= nums[i]) dp[i] = max(dp[i], dp[j] + 1); if (dp[i] > res) res = dp[i]; } return res; } }