如何找到最大的一些固定的给定长度的每个子阵列的给定数组中

编程入门 行业动态 更新时间:2024-10-28 16:30:00
本文介绍了如何找到最大的一些固定的给定长度的每个子阵列的给定数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们给出n个元素和整数k的阵列。假设我们希望长度为k的一个窗口在阵列上滑动,报告包含在每个窗口中的最大值。例如,给定阵列

15 10 9 16 20 14 13

由于长度为4的窗口,我们将输出

[15 10 9 16] 20 14 13 - >输出16 15 10 9 16 20] 14 13 --->输出20 15 10 9 16 20 14 13 - >输出20 15 10 9 16 20 14 13] --->输出20

因此​​,结果将是

16 20 20 20

我快到通过跟踪在每个点窗口的最大元素的问题,但遇到了一个问题,当最大的元素被滑出窗外。在这一点上,我想不出一个快速的方法来找出现存最大的因素是什么。

有谁知道一个高效的算法来解决这个问题?

解决方案

This旧的问题 讨论如何建立一个队列中的数据结构支持插入,出列,并找到分钟都在O(1)时间。注意,这是不会标准的优先级队列,而是在其中的任何一点,你可以找到它包含了O(1)时间的最小元素的值的队列。你可以很容易地修改这个结构,支持找到-Max在O(1),而不是找分钟,因为这是更贴近这个特殊的问题。

利用这种结构,可以按如下方式解决O(n)的这个问题时间:

  • 排队数组的第k个元素放入特殊的队列中。
  • 对于在阵列的其余部分的每个元素:
  • 使用队列的查找-MAX操作报告当前子范围的最大元素。
  • 出列从队列中的元素,而旧的范围的最后一个K-1的元素到位。
  • 排队从序列的下一个元素,从而导致队列现在持有该序列的下一个K-元件子范围。
  • 这总共需要O(n)的时间,因为你访问的每个数组元素一次,和入队各出队最多一次,调用find-MAX恰好n-k次。我认为这是pretty爽,因为复杂的独立的k个的,它不最初看起来像它一定是可能的。

    希望这有助于!并感谢问一个很酷的问题!

    We are given an array of n elements and an integer k. Suppose that we want to slide a window of length k across the array, reporting the largest value contained in each window. For example, given the array

    15 10 9 16 20 14 13

    Given a window of length 4, we would output

    [15 10 9 16] 20 14 13 ---> Output 16 15 [10 9 16 20] 14 13 ---> Output 20 15 10 [ 9 16 20 14] 13 ---> Output 20 15 10 9 [16 20 14 13] ---> Output 20

    So the result would be

    16 20 20 20

    I was approaching the problem by keeping track of the maximum element of the window at each point, but ran into a problem when the largest element gets slid out of the window. At that point, I couldn't think of a fast way to figure out what the largest remaining element is.

    Does anyone know of an efficient algorithm for solving this problem?

    解决方案

    This older question discusses how to build a queue data structure supporting insert, dequeue, and find-min all in O(1) time. Note that this is not a standard priority queue, but instead is a queue in which at any point you can find the value of the smallest element it contains in O(1) time. You could easily modify this structure to support find-max in O(1) instead of find-min, since that's more relevant to this particular problem.

    Using this structure, you can solve this problem in O(n) time as follows:

  • Enqueue the first k elements of the array into the special queue.
  • For each element in the rest of the array:

  • Use the queue's find-max operation to report the largest element of the current subrange.
  • Dequeue an element from the queue, leaving the last k-1 elements of the old range in place.
  • Enqueue the next element from the sequence, causing the queue to now hold the next k-element subrange of the sequence.
  • This takes a total of O(n) time, since you visit each array element once, enqueuing and dequeuing each at most once, and calling find-max exactly n-k times. I think this is pretty cool, since the complexity is independent of k, which doesn't initially seem like it necessarily should be possible.

    Hope this helps! And thanks for asking a cool question!

    更多推荐

    如何找到最大的一些固定的给定长度的每个子阵列的给定数组中

    本文发布于:2023-11-28 22:30:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1644195.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:定数   阵列   个子   组中   长度

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!