寻找Java数组中最长的向下序列

发布于 2024-09-26 13:57:18 字数 158 浏览 6 评论 0原文

给定这个数组,

int [] myArray = {5,-11,2,3,14,5,-14,2};

我必须能够返回 3,因为最长的向下序列是 14,5,-14。 最快的方法是什么?

PS:下降序列是一系列不递增的数字。

Given this array

int [] myArray = {5,-11,2,3,14,5,-14,2};

I must be able to return 3 because the longest down sequence is 14,5,-14.
What's the fastest way to do this?

PS: Down sequence is a series of non-increasing numbers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

强辩 2024-10-03 13:57:18

python 中的另一个实现:

def longest_down_sequence(seq):
    max = 0
    current_count = 0
    last = None
    for x in seq:
        if x <= last: current_count += 1
        else: current_count = 1
        if current_count > max: max = current_count
        last = x
    return max

another implementation in python:

def longest_down_sequence(seq):
    max = 0
    current_count = 0
    last = None
    for x in seq:
        if x <= last: current_count += 1
        else: current_count = 1
        if current_count > max: max = current_count
        last = x
    return max
梦幻之岛 2024-10-03 13:57:18

只需浏览一下数字列表即可。伪代码:

bestIndex = 0
bestLength = 0

curIndex = 0
curLength = 1

for index = 1..length-1
   if a[index] is less than or equal to a[index-1]
       curLength++
   else 
       //restart at this index since it's a new possible starting point
       curLength = 1
       curIndex = index

   if curLength is better than bestLength
       bestIndex = curIndex
       bestLength = curLength

next          

注意:如果您不关心知道子序列发生的位置,则可以放弃包含 bestIndex 或 curIndex 的任何行,如 Gary 的实现中所示。

Just make one pass through the list of numbers. Pseudocode:

bestIndex = 0
bestLength = 0

curIndex = 0
curLength = 1

for index = 1..length-1
   if a[index] is less than or equal to a[index-1]
       curLength++
   else 
       //restart at this index since it's a new possible starting point
       curLength = 1
       curIndex = index

   if curLength is better than bestLength
       bestIndex = curIndex
       bestLength = curLength

next          

Note: You can ditch any line containing bestIndex or curIndex if you don't care about knowing where that subsequence occurs, as seen in Gary's implementation.

无声静候 2024-10-03 13:57:18

在java中:

    int [] myArray = {5,-11,2,3,14,5,-14,2};
    int downSequence = 1;
    int longestDownSequence = 1;
    for(int i = 1; i < myArray.length; i++) {
        if(myArray[i] <= myArray[i-1]) downSequence++;
        else {
            if(downSequence > longestDownSequence)
                longestDownSequence = downSequence;
            downSequence = 1;
        }
    }
    if(downSequence > longestDownSequence)
        longestDownSequence = downSequence;
    System.out.println(longestDownSequence);

由于您要求最快或更好的性能,因此仅在重置计数器之前检查最长的下降序列。永远不会在每次迭代中。但是,您必须在循环后再次检查,以防最长的序列位于数组的末尾。

In java:

    int [] myArray = {5,-11,2,3,14,5,-14,2};
    int downSequence = 1;
    int longestDownSequence = 1;
    for(int i = 1; i < myArray.length; i++) {
        if(myArray[i] <= myArray[i-1]) downSequence++;
        else {
            if(downSequence > longestDownSequence)
                longestDownSequence = downSequence;
            downSequence = 1;
        }
    }
    if(downSequence > longestDownSequence)
        longestDownSequence = downSequence;
    System.out.println(longestDownSequence);

Since you're asking for fastest or better performance, only check for the longest down sequence just before you reset the counter. Never on each iteration. However, you have to check again after the loop in case the longest sequence is at the end of the array.

贱贱哒 2024-10-03 13:57:18

Java中的另一种解决方案:

static int[] longestDownSequenceList(int[] array) {

    if (array.length <= 1) {
        return array;
    }

    int maxSize = 1; 
    int maxEnd = 0; 

    int curSize = 1;

    for (int i = 1; i < array.length; i++) {

        if (array[i] < array[i-1]) {
            curSize++;

            if (curSize > maxSize) {
                maxSize = curSize; 
                maxEnd = i;
            }
        }
        else {               
            curSize = 1;
        }
    }

    return Arrays.copyOfRange(array, maxEnd-maxSize+1, maxEnd+1);
}

Another solution in Java:

static int[] longestDownSequenceList(int[] array) {

    if (array.length <= 1) {
        return array;
    }

    int maxSize = 1; 
    int maxEnd = 0; 

    int curSize = 1;

    for (int i = 1; i < array.length; i++) {

        if (array[i] < array[i-1]) {
            curSize++;

            if (curSize > maxSize) {
                maxSize = curSize; 
                maxEnd = i;
            }
        }
        else {               
            curSize = 1;
        }
    }

    return Arrays.copyOfRange(array, maxEnd-maxSize+1, maxEnd+1);
}
望笑 2024-10-03 13:57:18

正如比尔上面所说,这本质上是最长的递增子序列。请参阅维基百科条目以获取最佳解决方案。这是从那里引用的,进行了一些小的更改,以适用于非递减的情况,

 L = 0
 for i = 1, 2, ... n:
    binary search for the largest positive j ≤ L such that X[M[j]] >= X[i] (or set j = 0 if no such value exists)
    P[i] = M[j]
    if j == L or X[i] >= X[M[j+1]]:
       M[j+1] = i
       L = max(L, j+1)

请参阅上面我的评论中其他提议的解决方案的反例。

As Bill above said, this is essentially longest increasing subsequence. See the wikipedia entry for the optimal solution. This is quoted from there with small changes to work for the nondecreasing case

 L = 0
 for i = 1, 2, ... n:
    binary search for the largest positive j ≤ L such that X[M[j]] >= X[i] (or set j = 0 if no such value exists)
    P[i] = M[j]
    if j == L or X[i] >= X[M[j+1]]:
       M[j+1] = i
       L = max(L, j+1)

See counterexample to other proposed solution in my comment above.

转身以后 2024-10-03 13:57:18

最快的方法可能取决于环境:计算机和问题大小。

对于非常大的列表(或数组),并行化作业可能很有用,可以实现:

  • 将列表拆分为简单元素。
  • 将按顺序排列(或不增加)的元素粘合在一起,形成块,如果可能的话,将块粘合在一起。
  • 搜索最长的块。

The fastest way might depend on the environment: computer and problemsize.

For a very large List (or array) it might be useful to parallelize the job, which could be implemented:

  • Split and split and split the List to simple elements.
  • Glue together elements which are down sequences (or non increasing) to chunks, and glue together chunks, if possible.
  • Search for the longest chunk.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文