输入n个数,求出这个序列中最长的上升子序列的长度。

如:4 2 3 1 5;(2 3 5是最长上升子序列,长度为3)

#include <iostream>
#include <algorithm>
using namespace std;

int n;
const int maxn = 1000 + 20;
int a[maxn];
int dp[maxn];

/*
5
4 2 3 1 5
*/

void solve()
{
    int res = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        dp[i] = 1;
        for (int j = 0; j < i; j++)
        {
            if (a[j] < a[i]) 
            {
                dp[i] = max(dp[i], dp[j] + 1);
            }
        }
        res = max(res, dp[i]);
    }
    printf("%d\n", res);
}

int main()
{
    solve();
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2021-06-09
  • 2021-12-22
  • 2022-01-12
  • 2021-11-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-21
  • 2021-08-19
  • 2022-12-23
  • 2021-12-31
  • 2021-06-17
  • 2021-08-04
相关资源
相似解决方案