首页 > 代码库 > [算法]动态规划之最长递增子序列

[算法]动态规划之最长递增子序列

最长递增子序列

#include<stdio.h>#include<stdlib.h>#include<time.h>#include<string.h>#define N 4int solve(int *array, int n) {    int *dp = (int *)malloc(n * sizeof(int));    int i;    int j;    int result;    bzero((void *)dp, n * sizeof(int));        dp[0] = 1;    for(i = 1; i < n; i++) {        *(dp + i) = *(dp + i - 1);        for(j = 0; j < i; j ++) {            if(*(array + i) > *(array + j)) {                *(dp + i) = (*(dp + i) < *(dp + j) + 1) ? (*(dp + j) + 1) : *(dp + i);            }        }    }    result = *(dp + n -1);    free(dp);    return result;}int main(char ** args) {    srand((unsigned)time(NULL));    int *array = (int *)malloc(N * sizeof(int));    for(int i = 0; i < N; i++) {        *(array + i) = rand() % N;         printf("%d\t", *(array + i));    }    int ret = solve(array, N);    printf("\nresult:%d\n", ret);    free(array);    return 0;}

 

[算法]动态规划之最长递增子序列