用c实现数组最大不连续递增子序列数
时间: 2024-01-21 11:17:51 浏览: 84
以下是用C语言实现数组最大不连续递增子序列数的代码:
```c
#include <stdio.h>
int max(int a, int b) {
return a > b ? a : b;
}
int getMaxNonContiguousIncreasingSubsequence(int arr[], int n) {
int dp[n];
dp[0] = 1;
for (int i = 1; i < n; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (arr[j] < arr[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
int maxSeq = 0;
for (int i = 0; i < n; i++) {
maxSeq = max(maxSeq, dp[i]);
}
return maxSeq;
}
int main() {
int arr[] = {1, 3, 2, 4, 6, 5, 8, 7, 9};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Maximum length of non-contiguous increasing subsequence is %d\n", getMaxNonContiguousIncreasingSubsequence(arr, n));
return 0;
}
```
该算法的时间复杂度为O(n^2),其中n为数组的长度。
阅读全文