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

最长递增子序列

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

#define N 4

int 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;
}

 

posted @ 2014-12-15 13:51  ~嘉言懿行~~我是煲仔饭~~  阅读(183)  评论(0编辑  收藏  举报