# Longest Increasing Subsequence 最长上升子序列

输入: [10,9,2,5,3,7,101,18]
输出: 4

• 可能会有多种最长上升子序列的组合，你只需要输出对应的长度即可。
• 你算法的时间复杂度应该为 O(n2) 。

https://www.cnblogs.com/grandyang/p/4938187.html

    int lengthOfLIS(vector<int>& nums) {
vector<int> dp;
if (nums.size() <= 0) {
return 0;
}
dp.push_back(nums[0]);
for (int i = 1; i < nums.size(); i++) {
if (nums[i] < dp[0]) {
dp[0] = nums[i];
continue;
}
if (nums[i] > dp[dp.size() - 1]) {
dp.push_back(nums[i]);
continue;
}
int left = 0;
int right = dp.size()-1;
while (left < right) {
int mid = left + (right - left) / 2;
if (dp[mid] < nums[i]) {
left = mid + 1;
}
else {
right = mid;
}
}
dp[right] = nums[i];

}
return dp.size();
}

dp[i] = max(dp[j] + 1, dp[i]);

    int lengthOfLIS(vector<int>& nums) {
if(nums.size()==0){
return 0;
}
int *dp = new int[nums.size()];
int res = 1;
for (int i = 1; i < nums.size(); i++) {
dp[i] = INT_MIN;
}
dp[0] = 1;
for (int i = 1; i < nums.size(); i++) {
for (int j = 0; j <= i; j++) {
if (nums[j] < nums[i]) {
dp[i] = max(dp[j] + 1, dp[i]);
res = max(res, dp[i]);
}
}
dp[i]=dp[i]==INT_MIN?1:dp[i];
}

delete[] dp;
return res;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120