最大不下降子序列

#include <iostream>
using namespace std;

int main()
{   
# define N 5
    int arr[N] = {4,6,5,7,3};
    const int n = sizeof(arr) / sizeof(int);
    int dp[N] = {0};

    
    for (int i = 0; i < n; i++) {
        dp[i] = 1; // 默认长度是1,只有arr[i]自己
        for (int j = 0; j < i; j++) {
            if (arr[i] >= arr[j]) {
                if (dp[j]+1 > dp[i]) {
                    dp[i] = dp[j] + 1;
                }
            }
        }
    }
    
    for (int i = 0; i < N; i++) {
        cout << dp[i] << " ";
    }
    cout << endl;
    

    return 0;
}

发布了92 篇原创文章 · 获赞 2 · 访问量 3424

猜你喜欢

转载自blog.csdn.net/zxc120389574/article/details/105029387