leetcode_[python/C++]_300_Longest Increasing Subsequence
2016-11-13 17:31
459 查看
题目链接
【题目】
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 is [2, 3, 7, 101], therefore the length is 4. Note that 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?
【分析】
这道题最简单的思路就是O(n)算法,利用动态规划,新建一个dp数组
然后从前往后索引,对每一个位置i再从索引0到i-1,一旦找到一个比nums[i]小的值,索引为j,则将dp[j]+1与dp[i]比较取大的值
接下来我们来看O(nlog(n))的算法,利用二分查找的方法,在第二个循环中减少时间复杂度
网上有一种用到内置函数lower_bound()的做法,lower_bound()函数即求得第一个不小于nums[i]的值,跟上面的思想一致的
discuss上有这样写的:
python:
根据这个思想改进一下,可以少去每次计算len(dp)的时间,但在leetcode上显示不出差别的
【题目】
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 is [2, 3, 7, 101], therefore the length is 4. Note that 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?
【分析】
这道题最简单的思路就是O(n)算法,利用动态规划,新建一个dp数组
然后从前往后索引,对每一个位置i再从索引0到i-1,一旦找到一个比nums[i]小的值,索引为j,则将dp[j]+1与dp[i]比较取大的值
int lengthOfLIS(vector<int>& nums) { vector<int> dp(nums.size(),1); int ans = 0; for(int i=0;i<nums.size();i++){ for(int j = 0 ;j<i;j++){ if(nums[j]<nums[i]){ dp[i] = max(dp[i],dp[j]+1); } } ans = max(ans,dp[i]); } return ans; }
接下来我们来看O(nlog(n))的算法,利用二分查找的方法,在第二个循环中减少时间复杂度
class Solution { public: int lengthOfLIS(vector<int>& nums) { vector<int> dp; for(int i=0;i<nums.size();i++){ int right = dp.size(); int left = 0; while(left<right){ int mid = left + (right-left)/2; if(dp[mid]<nums[i]) left = mid + 1; else right = mid; } if(right>=dp.size()) dp.push_back(nums[i]); else dp[right] = nums[i]; } return dp.size(); } };
网上有一种用到内置函数lower_bound()的做法,lower_bound()函数即求得第一个不小于nums[i]的值,跟上面的思想一致的
class Solution { public: int lengthOfLIS(vector<int>& nums) { vector<int> dp; for(int i=0;i<nums.size();i++){ vector<int>::iterator iter = lower_bound(dp.begin(),dp.end(),nums[i]); if(iter == dp.end()){ dp.push_back(nums[i]); } else *iter = nums[i]; } return dp.size(); } };
discuss上有这样写的:
class Solution { public: int lengthOfLIS(vector<int>& nums) { vector<int> ans; for (int a : nums) if (ans.size() == 0 || a > ans.back()) ans.push_back(a); else *lower_bound(ans.begin(), ans.end(), a) = a; return ans.size(); } };
python:
def lengthOfLIS(self, nums): dp = [] for items in range(nums): left,right = 0,len(dp) while left<right: mid = left + (right-left)/2 if(dp[mid]<items): left = mid + 1 else: right = mid if right >= len(dp): dp.append(items) else: dp[right] = items return len(dp)
根据这个思想改进一下,可以少去每次计算len(dp)的时间,但在leetcode上显示不出差别的
def lengthOfLIS(self, nums): dp = [0] * len(nums) size = 0 for x in nums: i, j = 0, size while i != j: m = (i + j) / 2 if dp[m] < x: i = m + 1 else: j = m dp[i] = x size = max(i + 1, size) return size
相关文章推荐
- 第七周:(LeetCode 300) Longest Increasing Subsequence(c++)
- Leetcode 300 Longest Increasing Subsequence(Python)
- 【LEETCODE】300-Longest Increasing Subsequence [Python]
- LeetCode - 300 - Longest Increasing Subsequence
- leetcode——300—— Longest Increasing Subsequence
- 【LeetCode】300 Longest Increasing Subsequence
- [leetcode 300] Longest Increasing Subsequence
- leetcode 300 : Longest Increasing Subsequence
- LeetCode—300 Longest Increasing Subsequence
- [leetcode-300]Longest Increasing Subsequence(java)
- Leetcode 300 Longest Increasing Subsequence
- leetcode(300)—— Longest Increasing Subsequence(最长递增子序列)
- leetcode 354. Russian Doll Envelopes & leetcode 300 Longest Increasing Subsequence
- LeetCode 300: Longest Increasing Subsequence
- [leetcode300] Longest Increasing Subsequence
- Leetcode 300 Longest Increasing Subsequence
- leetcode 300: Longest Increasing Subsequence
- <LeetCode OJ> Longest Increasing Subsequence【300】
- Leetcode_300_Longest Increasing Subsequence
- Leetcode 300 Longest Increasing Subsequence 最长递增子序列