美文网首页C#题库
0040-最长上升子序列

0040-最长上升子序列

作者: 指尖极光 | 来源:发表于2017-03-25 15:36 被阅读65次

    问题描述

    一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的。对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列(ai1, ai2, ..., aiK),这里1 <= i1 < i2 < ... < iK <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 8)等等。这些子序列中最长的长度是4,比如子序列(1, 3, 5, 8). 你的任务,就是对于给定的序列,求出最长上升子序列的长度。

    输入

    输入的第一行是序列的长度N (1<=N<=1000)。第二行给出序列中的 N 个整数,这些整数的取值范围都在0到10000。

    输出

    最长上升子序列的长度。

    输入样列

    7
    1 7 3 5 9 4 8
    

    输出样例

    4
    

    算法实现

    using System;
    
    namespace Questions{
        class Program{
            public static void Main(string[] args){
                int n = int.Parse(Console.ReadLine());
                string input = Console.ReadLine();
                string[] data = input.Split(' ');
                int[] result = new int[n];
                int[] max = new int[n];
                for (int i = 0; i < n; i++)
                {
                    result[i] = int.Parse(data[i]);
                }
                int k = 0;
                for (int i = 0; i < n; i++)
                {
                    max[i] = 1;
                    for (int j = 0; j < i; j++)
                    {
                        if (result[i] > result[j])
                        {
                            max[i] = Math.Max(max[i], max[j] + 1);
                            k = i;
                        }
                    }
                }
                Console.WriteLine(max[k]);
                Console.ReadKey();
            }
        }
    }

    相关文章

      网友评论

        本文标题:0040-最长上升子序列

        本文链接:https://www.haomeiwen.com/subject/gjscottx.html