Returns the maximum number of consecutive days in a set of dates sorted from small to large.

Source: Internet
Author: User

Returns the maximum number of consecutive days in a set of dates sorted from small to large.
Specific Method:

static short Days()        {            var days = new List<DateTime>            {               Convert.ToDateTime("2016-12-01"),               Convert.ToDateTime("2016-12-04"),               Convert.ToDateTime("2016-12-06"),               Convert.ToDateTime("2016-12-08"),               Convert.ToDateTime("2016-12-09"),               Convert.ToDateTime("2016-12-12"),               Convert.ToDateTime("2016-12-13"),               Convert.ToDateTime("2016-12-14"),               Convert.ToDateTime("2016-12-16"),               Convert.ToDateTime("2016-12-17"),               Convert.ToDateTime("2016-12-18"),               Convert.ToDateTime("2016-12-19"),               Convert.ToDateTime("2016-12-21")            };            var counts = new List<short>();            short a = 0;            for (int i = 0; i < days.Count;)            {                if (a + 1 == days.Count) break;                short b = 0;                short max = 1;                for (int j = 0; j < days.Count; j++)                {                    var first = days[i].AddDays(j + 1);                    var second = days[a + 1];                    if (first == second)                    {                        max++;                        a++;                        b++;                    }                    else                    {                        a -= b;                        break;                    }                }                counts.Add(max);                a += max;                i += max;            }            return counts.Max();        }

Console output: Console. WriteLine (Days ());

// Output result:

// Algorithm principle:

// Compare the first element with the second element. if the date is different, the first element jumps out of the loop and continues the comparison from the next element.
// Compare the first element with the second element by adding one day. If the date is the same, the maximum number of consecutive days is increased by 1. Then, the second element is compared with the third element by adding one day, the date is the same plus 1. Execute the statement in sequence until two elements have different dates. Then, jump out of the loop and record the consecutive days of the cycle to the set.
// Add the element subscript to the maximum number of consecutive days of the previous cycle during the next cycle, skip the continuous element execution cycle, and reduce the number of cycles. For example, if the subscript of the fourth element () of a set is 3 and it is 4 consecutive with the fifth element (12-09 subscript is 4), then their consecutive days are 2,
// At the end of the loop, when the next loop is executed, add the subscript 3 of the first for with the consecutive number of days 2 to get 5, then, the loop starts from the sixth element, and the loop is skipped once in the middle. Similarly, if the number of consecutive days is 4, three cycles can be omitted.

// The language organization capability needs to be improved.> _ <!!!

Thank you for your comments!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.