解释山顶和标志算法

解释山顶和标志算法

本文介绍了解释山顶和标志算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改

刚指出,该要求规定峰不能阵列端。

所以,我跑过这个网站

,让你的编程问题,给你的证书,如果你能解决这些问题在2小时内。在第一个问题是一个我以前也见过,通常被称为峰和标志的问题。如果你不熟悉

一个非空的零索引数组A由N个整数给出。一峰比邻国更大的数组元素。更precisely,它是一个索引p,使得

。例如,下面的数组答:

  A [0] = 1
A [1] = 5
A [2] = 3
A [3] = 4
A [4] = 3
A [5] = 4
A [6] = 1
A [7] = 2
A [8] = 3
A [9] = 4
A [10] = 6
A [11] = 2
 

有正好四条峰:元件1,3,5和10

您会在行程中的山脉,其相对高度重新$ P $由阵列A psented你必须选择你要多少标志与你采取的。我们的目标是在设定的峰值标志的最大数量,按一定的规则。

标志只能在峰来设置。更重要的是,如果你取K标志,那么任何两个标志之间的距离应大于或等于K.指数P和Q之间的距离的绝对值|对 - Q |。

例如,鉴于数组A psented,上面有N = 12的山脉重$ P $,如果你把:

您可以因此,在这种情况下,设置最大的三个标志。

写功能,给定N个整数的一个非空的零索引数组A,返回可以在阵列的峰来设置的标志的最大数量。例如,给定上述

的阵列

函数应该返回3,如以上所解释

假设:

N是范围[1..100,000]内的整数;

数组A中的每个元素在该范围内的整数。[0..1,000,000,000]

复杂度:

预计最坏情况下的时间复杂度为O(N);预计最坏情况下的空间复杂度为O(N),超出输入存储(不包括所需的输入参数存储)。

输入数组的元素可以被修改。

所以,这是有道理的,但我失败了就用这个code

 公众诠释GetFlags(INT [] A)
{
        名单< INT> peakList =新的名单,其中,INT>();
        的for(int i = 0; I< =则为a.length  -  1;我++)
        {
                如果((A [I]>在[我+ 1]安培;&放大器; A [1]>在[我 -  1]))
                {
                    peakList.Add(ⅰ);
                }
        }

        名单< INT> flagList =新的名单,其中,INT>();
        INT距离= peakList.Count;
        flagList.Add(peakList [0]);
        对(INT I = 1,J = 0,最大值= peakList.Count; I&其中;最大; i ++在)
        {
            如果(Math.Abs​​(Convert.ToDecimal(peakList [J]) -  Convert.ToDecimal(peakList [I]))> =距​​离)
            {
                flagList.Add(peakList [I]);
                J =;
            }
        }
        返回flagList.Count;
}
 

修改

正确答案是3,但我的应用程序说2

这我不明白,既然有4个峰(指数1,4,6,8),并从,你应该能够放置一个旗2的峰值(1和6)

我失去了一些东西呢?显然,我的假设是,数组的开头或结尾可以是一个高峰,这​​是不是这样的?

如果这个需要去堆叠交换程序员,我会移动,但想到对话框这里将是有益的。

修改

解决方案

您的假设是错误的,因为峰定义为:

当涉及到你的第二个例子中,你可以设置3标记1,4,8。

EDIT

Just was pointed that the requirements state peaks cannot be ends of Arrays.

So I ran across this site

Which gives you programming problems and gives you certificates if you can solve them in 2 hours. The very first question is one I have seen before, typically called the Peaks and Flags question. If you are not familiar

A non-empty zero-indexed array A consisting of N integers is given. A peak is an array element which is larger than its neighbours. More precisely, it is an index P such that

.For example, the following array A:

A[0] = 1
A[1] = 5
A[2] = 3
A[3] = 4
A[4] = 3
A[5] = 4
A[6] = 1
A[7] = 2
A[8] = 3
A[9] = 4
A[10] = 6
A[11] = 2

has exactly four peaks: elements 1, 3, 5 and 10.

You are going on a trip to a range of mountains whose relative heights are represented by array A. You have to choose how many flags you should take with you. The goal is to set the maximum number of flags on the peaks, according to certain rules.

Flags can only be set on peaks. What's more, if you take K flags, then the distance between any two flags should be greater than or equal to K. The distance between indices P and Q is the absolute value |P − Q|.

For example, given the mountain range represented by array A, above, with N = 12, if you take:

You can therefore set a maximum of three flags in this case.

Write a function that, given a non-empty zero-indexed array A of N integers, returns the maximum number of flags that can be set on the peaks of the array.For example, given the array above

the function should return 3, as explained above.

Assume that:

N is an integer within the range [1..100,000];

each element of array A is an integer within the range [0..1,000,000,000].

Complexity:

expected worst-case time complexity is O(N);expected worst-case space complexity is O(N), beyond input storage (not counting thestorage required for input arguments).

Elements of input arrays can be modified.

So this makes sense, but I failed it using this code

public int GetFlags(int[] A)
{
        List<int> peakList = new List<int>();
        for (int i = 0; i <= A.Length - 1; i++)
        {
                if ((A[i] > A[i + 1] && A[i] > A[i - 1]))
                {
                    peakList.Add(i);
                }
        }

        List<int> flagList = new List<int>();
        int distance = peakList.Count;
        flagList.Add(peakList[0]);
        for (int i = 1, j = 0, max = peakList.Count; i < max; i++)
        {
            if (Math.Abs(Convert.ToDecimal(peakList[j]) - Convert.ToDecimal(peakList[i])) >= distance)
            {
                flagList.Add(peakList[i]);
                j = i;
            }
        }
        return flagList.Count;
}

EDIT

The correct answer is 3, but my application says 2

This I do not get, since there are 4 peaks (indices 1,4,6,8) and from that, you should be able to place a flag at 2 of the peaks (1 and 6)

Am I missing something here? Obviously my assumption is that the beginning or end of an Array can be a peak, is this not the case?

If this needs to go in Stack Exchange Programmers, I will move it, but thought dialog here would be helpful.

EDIT

解决方案

Your assumption is wrong since peak is defined as:

When it comes to your second example you can set 3 flags on 1, 4, 8.

这篇关于解释山顶和标志算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 22:00