最长算术子序列


11

给定一个非空的有限整数序列,返回最大长度的算术子序列。

如果有多个相同的最大长度,则可以返回它们中的任何一个。

定义:

一个算术序列是这样的序列a(1),a(2),a(3),a(4),...,例如,有一个恒定的c,使得a(m+1)-a(m) = c对于所有m。换句话说:两个后续项之间的差是恒定的。

给定一个序列,b(1),b(2),b(3),b(4),...一个序列就是一个序列b(s(1)),b(s(2)),b(s(3)),b(s(4)),...,其中1 <= s(1)s(m) < s(m+1)对于所有m。换句话说:采用原始顺序并删除所需的任意多个条目。

例子

Input                     Output
[4,1,2,3,6,5]             [1,3,5] or [1,2,3]
[5,4,2,-1,-2,-4,-4]       [5,2,-1,-4]
[1,2,1,3,1,4,1,5,1]       [1,1,1,1,1] or [1,2,3,4,5]
[1]                       [1]

一些较长的测试用例:

Length: 25
Input: [-9,0,5,15,-1,4,17,-3,20,13,15,9,0,-6,11,17,17,9,26,11,5,11,3,16,25]
Output: [15,13,11,9] or [17,13,9,5]

Length: 50
Input: [35,7,37,6,6,33,17,33,38,30,38,12,37,49,44,5,19,19,35,30,40,19,11,5,39,11,20,28,12,33,25,8,40,6,15,12,27,5,21,6,6,40,15,31,49,22,35,38,22,33]
Output: [6,6,6,6,6] or [39,33,27,21,15]

Length: 100
Input: [6,69,5,8,53,10,82,82,73,15,66,52,98,65,81,46,44,83,9,14,18,40,84,81,7,40,53,42,66,63,30,44,2,99,17,11,38,20,49,34,96,93,6,74,27,43,55,95,42,99,31,71,67,54,70,67,18,13,100,18,4,57,89,67,20,37,47,99,16,86,65,38,20,43,49,13,59,23,39,59,26,30,62,27,83,99,74,35,59,11,91,88,82,27,60,3,43,32,17,18]
Output: [6,18,30,42,54] or [8,14,20,26,32] or [46,42,38,34,30] or [83,63,43,23,3] or [14,17,20,23,26] or [7,17,27,37,47] or [71,54,37,20,3]

背景

当我回想起2004年的Green-Tao-定理时,我有了这个想法,该定理指出素数序列包含任意长度的有限算术序列。

Answers:


5

果冻,8 字节

ŒPIE$ÐfṪ

在线尝试!验证所有测试用例

这个怎么运作

ŒPIE$ÐfṪ  Main link. Argument: A (list of integers)

ŒP        Powerset; generate all sublists of A, sorted by length.
     Ðf   Filter the powerset by the link to the left:
    $       Combine the two atoms to the left into a monadic link.
  I           Compute all increments.
   E          Test whether they're all equal.
          This returns all arithmetic subsequences, sorted by length.
       Ṫ  Tail; extract the last sequence.

2

Pyth,12 11字节

ef!P{-VTtTy

测试套件。

          y  powerset of implicit input, generate all subsequences
ef       T   find the last subsequence (sorted increasing in length) where...
       Tt      bifurcate over tail, giving [1,2,3,4,5] [2,3,4,5]
     -V        vectorize over -, giving differences of each consecutive pair
    {          dedup (remove duplicate elements)
   P           pop, resulting in [] if all differences were equal
  !            boolean not, resulting in True if all differences were equal

感谢@LeakyNun一个字节!


2

MATL,19 18 17 16 18字节

路易斯节省了1个字节(并增加了2个字节)!

"GX@XN!"@dun2<?vx@

一种相当幼稚的方法,蛮力检查输入的所有有序排列。显然,这可能需要更长的时间。为了节省一个字节,我从最小的子序列(长度= 1)开始,一直到更大的序列(长度= N)。

在线尝试!

说明

                % Impilicitly grab input array (N)
"               % For each value in this array
    G           % Explicitly grab the input
    X@          % Loop index, will be [1, 2, ... length(N)] as we iterate
    XN          % Determine all permutations of k elements (nchoosek). The output is 
                % each k-length permutation of the input as a different row. Order doesn't 
                % matter so the result is always ordered the same as the input
    !           % Take the transpose so each k-length permutation is a column
    "           % For each column
        d       % Compute the difference between successive elements
        un      % Determine the number of unique differences
        2<?     % If there are fewer than 2 unique values
            vx  % Vertically concatenate everything on the stack so far and delete it
            @   % Push the current permuatation to the stack
                % Implicit end of if statement
                % Implicit end of for loop
                % Implicit end of for loop
                % Implicitly display the stack

@LuisMendo谢谢!我一直想知道如何获取循环迭代号。
2016年

@LuisMendo哦,很好,您是对的。该double diff给出了一个不可取的空数组。
Suever,2016年

1

Python 2,124 115 98 97字节

p=[[]]
for n in input():p+=[x+[n][:2>len(x)or n-x[-1]==x[1]-x[0]]for x in p]
print max(p,key=len)

非常慢且占用大量内存。在Ideone上进行测试

备用版本,98字节

p={()}
for n in input():p|={x+(n,)[:2>len(x)or n-x[-1]==x[1]-x[0]]for x in p}
print max(p,key=len)

这将立即完成所有测试用例。在Ideone上进行测试


1
字节或速度,这就是问题
downrep_nation

0

Pyth 结帐8593c76,3月24日,10个字节

efq-VTtT)y

这与Doorknob的答案完全相同,只是在进行中,有一个2字节的函数(q ... ))检查列表中的所有元素是否都相同,这与相同!P{,这是您可以做的最好的事情目前。


0

JavaScript(ES6),157个字节

a=>{for(m=i=0,l=a.length;i<l;i++)for(j=i;++j<l;)for(t=n=a[k=i],c=0;k<l;k++)a[k]==t&&(t+=a[j]-n,++c>m)?q=a[m=c,p=n,j]-n:q;return a.slice(-m).map(_=>(p+=q)-q)}

几乎是果冻答案的20倍...

function subsequence(a) {
    var max = 0;
    for (var i = 0; i < a.length; i++) {
        for (var j = i + 1; j < a.length; j++) {
            var target = a[i];
            var count = 0;
            for (var k = i; k < a.length; k++) {
                if (a[k] == target) {
                    count++;
                    target += a[j] - a[i];
                    if (count > max) {
                        max = count;
                        start = a[i];
                        step = a[j] - a[i];
                    }
                }
            }
        }
    }
    var result = new Array(max);
    for (var i = 0; i < max; i++) {
        result[i] = start + step * i;
    }
    return result;
}
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.