N 個の整数のシーケンスが与えられます。順列を形成する隣接する数の最長部分列の長さを見つける方法。O(N^2) アルゴリズムしか見つかりませんでした。O(NlogN) または O(N) のソリューションが必要だと思います。それらのいずれかを提案してください。
この数列の答え1 3 1 2 5
は 3 (4 1 3 1 2 5 )
これは SPOJ 問題LPERMUTです。
O(N^2) アルゴリズム
Foreach i find the sum of first i elemetns: Sum(i)
Foreach i find the last element with indexes 1 .. i-1 which is equal to i-rd element: Prev(i)
Foreach i iterate j throw i+1 .. N while Prev(j) equals between i and j.
//So we get that all elemnent between i and j are distinct.
Then check if sum of them is equal to 1+2+...(j-i+1).
If it occurs that elements are permutation.
Sum of the elements between i and j we can get by Sum(j) - Sum(i-1)