提取局部最大值


19

给定一个正整数数组,输出一个大于或等于相邻元素的所有元素的数组。大多数元素将具有两个相邻的元素。第一个和最后一个元素是特殊情况,因为它们只有一个相邻元素。

您可以假定该数组至少包含两个元素。

测试用例:

Input               | Output
[4,2,6,12,4,5,4,3]  | [4,12,5]
[1,2]               | [2]
[1,2,3,2,1]         | [3]
[3,2,1,2,3]         | [3,3]
[4,4]               | [4,4]
[2,4,4,4,1]         | [4,4,4]
[2,3,3,4]           | [3,4]
[4,3,3,4]           | [4,4]

这是,最短的代码获胜!


1
@PeterTaylor我认为这是“要在输出中包含第一个或最后一个元素,...”
xnor

@PeterTaylor xnor是正确的。
帕维尔


还与之相关:寻找局部极端
Wrzlprmft

我可以提出[4,3,3,4]一个测试用例吗?可悲的是,我的解决方案无法很好地解决这一问题。
JAD

Answers:


5

果冻 13 12  11 字节

0;;0»3\f"⁸Ẏ

单链链接,该列表采用正整数列表,并返回仅包含大于或等于所有邻居的整数的过滤列表。

在线尝试!


12位

0;INżI>0ḄNMị

13位

0;;0ṡ3M€ċ€2Tị

怎么样?

0;;0»3\f"⁸Ẏ - Link: list of positive integers, A
0;          - a zero concatenated with A
  ;0        - concatenate a zero
     3\     - 3-wise reduce with:
    »       -   maximum (yields a list of the maximums in each overlapping window of 3)
         ⁸  - chain's left argument, A
        "   - zip with:
       f    -   filter keep (i.e. keep the maximal if it is [in] the [length 1 list 
            -                     of the] respective original element)
          Ẏ - flatten by one level

好吧,我认为可能有一种方法可以使用3倍减法,但是我还没有解决。
乔纳森·艾伦

我是对的-以最大二倍数进行3倍减少»-尽管10 ..
乔纳森·艾伦

8

Python,54个字节

f=lambda l,*p:l and l[:p<=l[:1]>=l[1:2]]+f(l[1:],l[0])

在线尝试!

I / O是元组而不是列表。


Python,57字节

f=lambda l,p=0:l and l[:[p]<=l[:1]>=l[1:2]]+f(l[1:],l[0])

在线尝试!

Alt 57:

f=lambda l,p=0:l and l[l<[max(p,*l[:2])]:1]+f(l[1:],l[0])


6

Haskell,50 49 42字节

f l=[j|i:j:k:_<-scanr(:)[0]$0:l,k<=j,i<=j]

在线尝试!

scanr(:)[0]使得尾部的列表(0:l),每一个最终0,例如用于l = [4,3,3,4][[0,4,3,3,4,0],[4,3,3,4,0],[3,3,4,0],[3,4,0],[4,0],[0]]其是图案匹配agains i:j:k:_提取所有列表与被命名为至少3种元素ijkj如果其> = i和,则保留j

编辑:ØrjanJohansen保存了7个字节。谢谢!


2
i:j:k:_<-scanr(:)[0]$0:l更短。(略微调整“标准” tails=scanr(:)[]技巧。)
ØrjanJohansen

@ØrjanJohansen:哦,我在我之前就已经使用过这个技巧,但是在某种程度上错过了它。非常感谢!
nimi

4

Dyalog APL,31 30 28 22 21位元组

{⍵/⍨(⌈/=2⌷⊢)¨3,/∊0⍵0}

在线尝试!

说明(我不擅长解释事物):

0⍵0       - [0,input,0]   (it looks like a face!)
∊         - flatten
3,/       - split into overlapping sections of length 3.
(⌈/=2⌷⊢)¨ - Whether the middle element is the maximum (applied to every section)
⍵/⍨       - index


3

JavaScript(ES6),40个字节

a=>a.filter((e,i)=>!(e<a[i-1]|e<a[i+1]))

3

Python 3中84个75 * 71字节

lambda l,k=[0]:[j for x,j in enumerate(l)if(k+l+k)[x+2]<=j>=(k+l+k)[x]]

在线尝试!


* @ LeakyNun使用巧妙的运算符技巧保存了9个字节。


lambda l,k=[0]:[l[i]for i in range(len(l))if(k+l+k)[i+2]<=l[i]>=(k+l+k)[i]]
Leaky Nun


2

05AB1E15  14  13字节

ü‹0¸«sĆÁü›+_Ï

在线尝试!

说明

ü‹             # pairwise comparison for less than
  0¸«          # append 0
     s         # swap input to top of stack
      Ć        # enclose, append the head of the list
       Á       # rotate right
        ü›     # pairwise comparison for greater than
          +    # add the two boolean lists
           _   # logical negate
            Ï  # keep only elements of input that are true in the resulting list

以前的15字节解决方案

¬s¤)˜Œ3ùεZQ1è}Ï

在线尝试!

说明

¬                # get head of input
 s¤              # get tail of input
   )˜            # wrap stack in flattened list
                 # produces the input list with the first and last element duplicated
     Œ3ù         # push sublists of length 3
        ε        # apply transformation on each triple
         ZQ      # ... check each element for equality to the max
          1è     # ... get the middle element
            }    # end transform
             Ï   # keep only elements of input that are true in the resulting list

2

R,44个字节

pryr::f(x[(x>=c(0,x)&x>=x[-1])[1:sum(x|1)]])

计算结果为:

function (x) 
x[(x >= c(0, x) & x >= x[-1])[1:sum(x | 1)]]

比较xc(0,x),所以用x移动一个位置向右。还比较xx[-1],所以一个位置移位到左侧。TRUE如果都存在最大值,则这两个都是。&取这些布尔值的AND。由于R向量的长度不同时的包装性质,我们必须将结果截短为R的长度x,可通过取sum(x|1)。然后,我们插入布尔向量,仅获取的真实索引x并返回。

注意,由于这些逻辑运算是用不等长的向量完成的,因此R会抱怨。很多。但是在警告中会出现正确的输出:

> pryr::f(x[(x>=c(0,x)&x>=x[-1])[1:sum(x|1)]])(c(4,2,6,12,4,5,4,3))
[1]  4 12  5
Warning messages:
1: In x >= c(0, x) :
  longer object length is not a multiple of shorter object length
2: In x >= x[-1] :
  longer object length is not a multiple of shorter object length
3: In x >= c(0, x) & x >= x[-1] :
  longer object length is not a multiple of shorter object length



1

R,68个字节

function(a)a[a==sapply(1:length(a),function(i)max(c(0,a,0)[i+0:2]))]

在线尝试!


pryr::f(expression)比声明函数更短的方法function(a)expression
JAD

另外,sum(a|1)是的快捷方式length(a)
JAD

请参阅我的解决方案以获取更短的方法。
JAD




1

Stax,10 个字节

úâH◄(☼bM•Å

运行并调试

它在标准输出上将输出生成为换行符分隔的值。

拆开包装,松开包装并进行评论,看起来像这样。

f       filter each value in input using the rest of the program; implicitly printing kept values
  x0|S  input pre- and post-pended with zero
  3B    split into batches of 3
  i@    get the i-th batch, where i is the iteration index
  |M=   is the current value equal to the max from the batch?

运行这个

更新:刚刚找到一个9字节的解决方案。稍后将更新说明:

Stax,9 个字节

▀▓ûa¥╓╧↨⌐

运行并调试


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.