300. Longest Increasing Subsequence
Posted jtechroad
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了300. Longest Increasing Subsequence相关的知识,希望对你有一定的参考价值。
// O(nlogn) class Solution { public: int lengthOfLIS(vector<int>& nums) { int n = nums.size(); if (n < 1) return 0; vector<int> dp; dp.push_back(nums[0]); for (int i = 1; i < n; i++) { auto p = lower_bound(dp.begin(), dp.end(), nums[i]); if (p == dp.end()) dp.push_back(nums[i]); else *p = nums[i]; } return dp.size(); } }; /* // O(n*n) class Solution { public: int lengthOfLIS(vector<int>& nums) { int n = nums.size(); if (n < 1) return 0; vector<int> dp(n, 1); int res = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) if (nums[i] > nums[j]) dp[i] = max(dp[i], dp[j] + 1); res = max(res, dp[i]); } return res; } }; */
以上是关于300. Longest Increasing Subsequence的主要内容,如果未能解决你的问题,请参考以下文章
300. Longest Increasing Subsequence
300. Longest Increasing Subsequence
300. Longest Increasing Subsequence
300. Longest Increasing Subsequence