解释山顶和标志算法山顶、算法、标志

2023-09-11 04:24:17 作者:嘟着小嘴耍任性

修改

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

所以,我跑过这个网站

  

http://codility.com/

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

JVM 的三色标记算法详解

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

  

0℃下P< N - 1和A [P - 1]; A [P]> A [P + 1]

。 例如,下面的数组答:

  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 $,如果你把:

  

两个标志,你可以在峰1和5设置它们;

     

三旗,你可以在峰1,5和10集他们;

     

四旗,你可以只设置三旗,上峰1,5和10。

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

写功能,给定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;
}
 

修改

  

INT [A =新INT [] {7,10,4,5,7,4,6,1,4,3,3,7};

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

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

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

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

修改

解决方案   

显然,我的假设是,数组的开头或结尾即可   是一个高峰,这​​是不是这样的?

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

  

0℃下P< N - 1

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

EDIT

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

So I ran across this site

http://codility.com/

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

0 < P < N − 1 and A[P − 1] < A[P] > A[P + 1]

. 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:

two flags, you can set them on peaks 1 and 5;

three flags, you can set them on peaks 1, 5 and 10;

four flags, you can set only three flags, on peaks 1, 5 and 10.

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 the storage 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

int[] A = new int[] { 7, 10, 4, 5, 7, 4, 6, 1, 4, 3, 3, 7 };

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

解决方案

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

Your assumption is wrong since peak is defined as:

0 < P < N − 1

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