最長算術部分列


11

整数の空でない有限シーケンスを指定すると、最大長の算術サブシーケンスを返します。

同じ最大長の倍数がある場合、それらのいずれかを返すことができます。

定義:

演算シーケンスは、シーケンスであるa(1),a(2),a(3),a(4),...定数が存在するように、cそのようなa(m+1)-a(m) = cすべてのためにm。つまり、次の2つの用語の差は一定です。

シーケンスを考えるとサブはシーケンスであると、すべてのために。つまり、元のシーケンスを取り、必要な数のエントリを削除します。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-Theoremを思い出したときにこの考えを得ました。これは素数のシーケンスには任意の長さの有限の算術シーケンスが含まれていると述べています。

回答:


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バイト

Luisのおかげで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ありがとう!ループの繰り返しを取得する方法はいつも疑問でした。
スーバー

@LuisMendoいいね、あなたは正しい。そのdouble diffは、無効にできない空の配列を提供します。
スーバー

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の答えとまったく同じですが、3月に戻ってq ... )、リストのすべての要素が同じであるかどうかをチェックする2バイトの関数()!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倍長い... Ungolfed:

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;
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.