求数组的最大递增子序列/最大递减子序列

/// 求数组的最大递增子序列/最大递减子序列

#include <bits/stdc++.h>
using namespace std;

int LIS(vector<int> vec)
{
    int res = 0;
    int sz = vec.size();
    vector<int> dp(sz +1, 0);
    dp[0] = 1;
    for(int i = 1; i < sz; i++)
    {
        for(int j = 0; j < i; j++)
        {
            if(vec[j] < vec[i]){ 
                if(dp[i] < dp[j] + 1) {
                    dp[i] = dp[j] + 1;
                }
            }
        }
        res = max(res, dp[i]);
    }
    return res;
}

int main()
{
    vector<int> array= {1,4,5,6,2,3,8,9,10,11,12,12,1};
    int res = LIS(array);
    cout << res <<endl;
    return 0;
}
posted @ 2019-09-07 16:18  lllittletree  阅读(552)  评论(0编辑  收藏  举报