确定最宽的山谷


12

想象一下,我们得到了一些山区的一片,这将导致类似于以下的形状:

4                   _
3   _    _       __/ \
2  / \__/ \    _/     \_   /
1 /        \  /         \_/
0           \/
  12322223210012233343221112

如我们所见,我们可以(在一定程度上)用整数序列表示这一点。

出于此挑战的目的,我们将山谷定义为一个连续的子序列,在该子序列中,值最初是减小的,从某个点开始它们是增大的。更正式地为一个序列(ai)i=1n的波谷将指数1s<r<tn的量,下式成立:

  • 山谷的起点和终点相同:as=at
  • 谷开始和结束一次的区域变得更低:as>as+1at1<at
  • 谷是不平坦的:asararat
  • 谷最初降低:i[s,r):aiai+1
  • 在一些点增加谷意愿:j[r,t):ajaj+1

现在,我们将这种谷的宽度定义为指标[s,t]的大小。ts+1

挑战

给定一个高度轮廓(非负整数的序列),您的任务是确定最宽谷的宽度。

给定高度轮廓[1,2,3,2,2,2,2,3,2,1,0,0,1,2,2,3,3,3,4,3,2,2,1,1,1,2],我们可以像以前一样对其进行可视化:

4                   _
3   _    _       __/ \
2  / \__/ \    _/     \_   /
1 /        \  /         \_/
0           \/
  12322223210012233343221112
    aaaaaa             ccccc
         bbbbbbbbb

请注意,第二个谷如何[3,2,1,0,0,1,2,2,3]不向右延伸,因为最左边的点是3而不是4。此外,我们不将剩余的两个3 s 相加,因为我们要求端点高于倒数第二个点。

因此,最宽谷的宽度为9

规则

  • 输入将是一个非负(抱歉的荷兰人)整数序列
    • 您可以假设始终至少有一个山谷
  • 输出将是上面定义的最宽谷的大小

测试用例

[4,0,4] -> 3
[1,0,1,0,1] -> 3
[1,0,2,0,1,2] -> 4
[13,13,13,2,2,1,0,1,14,2,13,14] -> 4
[1,2,3,2,2,2,2,3,2,1,0,0,1,2,2,3,3,3,4,3,2,2,1,1,1,2] -> 9
[3,2,0,1,0,0,1,3] -> 4

2
测试案例:[3,2,0,1,0,0,1,3]。当前所有答案均返回8,根据您的定义,我认为应该是
4。– Zgarb

这座山的坡度会不会比1陡?(例如[3,1,2,3]
门把手

@Zgarb:是的,是的。我将其添加到测试用例中。
ბიმო

@Doorknob:没有什么可以阻止的,是的。例如[4,0,4]将是这种情况。
ბიმო

1
输入将是一个非负数(对不起的荷兰人)整数的序列 ”大声笑,当我读到这篇荷兰语时我咯咯笑了。;)
Kevin Cruijssen

Answers:


3

果冻,15 个字节

ẆµIṠḟ0ṢƑaMIḢ)Ṁ‘

在线尝试!

或查看一个测试套件(添加了另外两个我以前无法实现的测试用例)。

怎么样?

ẆµIṠḟ0ṢƑaMIḢ)Ṁ‘ - Link: list of integers
Ẇ               - all contiguous substrings
 µ          )   - for each substring, X:
  I             -   deltas
   Ṡ            -   sign (-ve:-1, 0:0, +ve:1)
    ḟ0          -   filter out zeros
       Ƒ        -   is invariant under:
      Ṣ         -     sort
         M      -   get maximal indices of X
        a       -   (vectorising) logical AND
          I     -   deltas
           Ḣ    -   head
             Ṁ  - maximum
              ‘ - increment

3

JavaScript(ES6),111 108 99 97字节

a=>a.map(o=(p,x)=>a.some(P=q=>(~x?x<0?i?q<P:q>P&&i++:i=0:q>=p)||(o=o<--x|q==P|q-p?o:x,P=q,0)))|-o

在线尝试!

已评论

a =>                        // a[] = input array
  a.map(o =                 // initialize the output o to a non-numeric value
    (p, x) =>               // for each value p at position x in a[]:
    a.some(P =              //   initialize P to a non-numeric value
      q =>                  //   for each value q in a[]:
      (                     //     exit if something goes wrong:
        ~x ?                //       if x is not equal to -1:
          x < 0 ?           //         if x is negative:
            i ?             //           if we're in the increasing part:
              q < P         //             exit if q is less than P
            :               //           else:
              q > P && i++  //             increment i if q is greater than P
          :                 //         else:
            i = 0           //           initialize i to 0 (decreasing part)
        :                   //       else:
          q >= p            //         exit if q is greater than or equal to p
      ) || (                //     if we didn't exit:
        o =                 //       update the output o:
          o < --x |         //         decrement x; if o is less than x
          q == P |          //         or the last value is equal to the previous one
          q - p ?           //         or the last value is not equal to the first one
            o               //           leave o unchanged
          :                 //         else:
            x,              //           update o to x
        P = q,              //       update the previous value P to q
        0                   //       force this iteration to succeed
      )                     //
    )                       //   end of some()
  ) | -o                    // end of map(); return -o

3

Python 2中120个 115 89 87 86 152 149字节

lambda a:max(r-l+1for l,v in e(a)for r,w in e(a)if max(a[l+1:r]+[0])<v==w*any(s(a[l:i])[::-1]+s(a[i:r])==a[l:r]for i,_ in e(a)));e=enumerate;s=sorted

在线尝试!


1

视网膜0.8.2,77字节

\d+
$*
M&!`\b(1+),((?!\1)(?!1+\2)1*,)+((?!\1)1*(?(3)\3|\2))*\1\b
1

O^`
\G,|$

在线尝试!链接包括测试用例。说明:

\d+
$*

转换为一元。

M&!`

列出而不是计算重叠的匹配项。

\b(1+),

山谷的起点被捕获为\1。然后,它必须直到结束才再次匹配。由于我们没有捕获逗号,因此也阻止了更高的值匹配。

((?!\1)(?!1+\2)1*,)+

匹配递减值。在(?!1+\2)防止通过环路中的任何通从大于先前的。(\2没有设置第一次通过,因此无法进行简单匹配。)捕获内容包括尾随逗号,因为这是高尔夫球手。

((?!\1)1*(?(3)\3|\2))*

匹配增加的值。这个时间((?3)\3|\2)意味着每次匹配都必须至少与前一个值一样长,或者最后一次递减是通过循环的第一次。

\1\b

最后,山谷的尽头必须与起点相同。

1

删除高度,保留逗号。(这比计算高度要容易一些,因为其中一些高度可能为零。)

O^`

以相反的顺序排序,即最逗号开头。

\G,|$

计算第一行的逗号数量,再加上一个。


1

外壳,13个字节

→▲mΓ€fȯΛEtġ≤Q

在线尝试!

说明

我使用与Jonathan Allan类似的算法。

→▲mΓ€fȯΛEtġ≤Q  Input is a list, say [3,1,0,1,1,0,2,3]
            Q  Nonempty slices: [[3],[1],[3,1],[0],...,[3,1,0,1,1,0,2,3]]
     f         Keep those that satisfy this:
                Argument is a slice, say [3,1,0,1,1,0,2]
          ġ≤    Cut into non-increasing pieces: [[3,1,0],[1,1,0],[2]]
         t      Drop first piece: [[1,1,0],[2]]
      ȯΛ        Each remaining piece
        E       has all elements equal: false, [1,1,0] has different elements
  m            Map over remaining slices:
                Argument is a slice, say [1,0,1,1]
   Γ            Break into head 1 and tail [0,1,1]
    €           Index of first occurrence of head in tail: 2
 ▲             Maximum: 2
→              Increment: 3

0

Japt,31个字节

¡ãYÄÃrc k_ò< Åd_äa x}îbZvÃrÔ+2

在线尝试!

从Zgarb的Husk答案中汲取灵感,节省了10个字节。我仍然认为这可以改善,但是我还没有发现。

说明:

¡ãYÄÃrc                            Get all segments
        k_           Ã             Remove ones where:
          ò<                        A non-increasing sub-segment
             Å                      Other than the first one
              d_äa x}               Has different heights
                      ®   Ã        For each remaining segment:
                       bZv          Get the second index of the first character
                           rÔ      Maximum
                             +2    Increase by 2
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.