c++给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。 子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。
时间: 2024-03-18 12:42:06 浏览: 74
这是一个经典的动态规划问题,可以使用 O(n^2) 或者 O(nlogn) 的算法解决。
O(n^2) 的算法思路是定义一个 dp 数组,其中 dp[i] 表示以 nums[i] 结尾的最长严格递增子序列的长度。初始化 dp 数组为 1,即每个元素本身就是一个长度为 1 的严格递增子序列。然后从前往后遍历数组,对于每个位置 i,枚举其前面的所有位置 j,如果 nums[j] < nums[i],则更新 dp[i] = max(dp[i], dp[j] + 1)。
O(nlogn) 的算法思路是使用贪心+二分查找的方法。定义一个 tails 数组,其中 tails[i] 表示长度为 i+1 的严格递增子序列的最后一个元素的最小值。初始化 tails 数组为一个只包含 nums[0] 的数组。然后从第二个元素开始遍历数组,对于每个元素 nums[i],如果 nums[i] > tails[len-1],则将 nums[i] 添加到 tails 数组的末尾,长度加 1;否则在 tails 数组中二分查找第一个大于等于 nums[i] 的位置 j,将 tails[j] 替换为 nums[i]。最终 tails 数组的长度就是最长严格递增子序列的长度。
以下是 O(n^2) 的 C++ 代码实现:
```c++
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n, 1);
int ans = 1;
for (int i = 1; i < n; 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(nlogn) 的 C++ 代码实现:
```c++
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
vector<int> tails;
tails.push_back(nums[0]);
for (int i = 1; i < n; i++) {
if (nums[i] > tails.back()) {
tails.push_back(nums[i]);
} else {
int left = 0, right = tails.size() - 1;
while (left < right) {
int mid = (left + right) / 2;
if (tails[mid] < nums[i]) {
left = mid + 1;
} else {
right = mid;
}
}
tails[left] = nums[i];
}
}
return tails.size();
}
```
阅读全文
相关推荐


















