Tuesday, January 27, 2015

[Algorithm]Maximum and Minimum in Window of Numbers


Given an array of integer elements and an integer d please consider all the sequences of dconsecutive elements in the array. For each sequence we compute the difference between the maximum and the minimum value of the elements in that sequence and name it the deviation.
Your task is to
  • write a function that computes the maximum value among the deviations of all the sequences considered above
  • print the value the standard output (stdout) 
本质上是窗口最大最小值的问题,我们很自然的先想到应该用heap,时间复杂度是O(n log n),这是显而易见的解法。但实际上,这一题我们可以到达O(n)的时间复杂度,我们用双向队列,以窗口最大值为例,假设窗口大小为k,队列的最大size是k,我们队列里维护的从头到尾是从旧到新,并且是严格递减序列,来了一个新值的话,我们做以下操作:

  • 如果队尾元素小于等于当前元素,我们一直dequeue直到队列或者队尾元素大于当前元素,然后从队尾入队
  • 如果对头元素在窗口范围外了,将队头元素出队
我们会在两端出队,在队尾入队,窗口最小值的话是类似的。每个元素最多入队一次出队一次,时间复杂度O(n),代码如下:

No comments:

Post a Comment