zoukankan      html  css  js  c++  java
  • The longest plateau

    Problem:

    Given an array, try to develop an efficient algorithm which can compute the length of the longest plateau. A plateau is a consecutive segment of an array with equal contents. For example, if x[] = {1, 2, 3, 4, 4, 4, 5, 5, 6}, then we have six plateaus which are 1, 2, 3, 4-4-4, 5-5 and 6. And obviously the length of the longest plateaus is 3.

    Analysis:

    Well, a straightforward idea is try to firstly compute all the length of different plateaus from left to right and then select the longest length. The pseudo-code is like this:

    for each element in the array a[]
         if a[i] is equal to a[i-1]
              add 1 to the length for the current plateau
              check whether the current length is the longest one so far
         else
              reset length to 1 // plateau length is at least 1

    Whether we need line 5&6 depends on whether we need to store the length of every plateau. If we just want to calculate the longest length then we can keep the code and use the “length” as a temp variable which is only used inside the loop. On the other hand, if we need to keep track of the length of all plateaus, we need to use an array of “length[]” to store the needed information.

    /*
     * input: an array a[], the length of the array n
     * output: the length of the longest plateau
     */
    int longestPlateau (int a[], int n)
    {
        if (n == 0)
            return 0;
     
        int length = 1;
        int longestLength = 1;
     
        for (int i = 1; i<n; i++)
        {
            if (a[i] == a[i-1])
            {
                length++;
                longestLength = max(longestLength, length);
            }
            else
                length = 1;
        }
        return longestLength;
    }

    Some more:

    What if the given array is sorted (in the increasing order) already?

    Actually if the array is sorted, the algorithm can be much simpler:

    assume the longest length now is L, then we just need to compare a[i] and a[i-L], if they are equal then all the elements between them are also equal (since this is a sorted array!), and we can add 1 to the current longest length. The code looks like this:

    /*
     * input: an sorted array a[] (increasing order), the length of the array n
     * output: the length of the longest plateau
     */
    int longestPlateau (int a[], int n)
    {
        if (n == 0)
            return 0;
     
        int length = 1;
        for (int i = 1; i<n; i++)
        {
            if (a[i] == a[i-length])
                length++;
        }
        return length;
    }
  • 相关阅读:
    Python--day43--mysql自增列之起始值和步长
    Python--day43--补充之主键和外键
    Python--day42--MySQL外键定义及创建
    Python--day42--mysql操作数据库及数据表和基本增删改查
    Python--day42--mysql创建用户及授权
    sql数据库基础
    Python--day41--线程池--python标准模块concurrent.futures
    C# 第三方DLL,可以实现PDF转图片,支持32位系统、64位系统
    ASP.NET 使用Session,避免用户F5刷新时重复提交(转)
    (重要,部署和发布)c# webApi 服务端和客户端 详细实例
  • 原文地址:https://www.cnblogs.com/zhangjie/p/3340954.html
Copyright © 2011-2022 走看看