您的位置:首页 > 其它

找出子数组中最大值最小值差值的最大值

2014-12-02 21:39 239 查看
一个题目,据说来自Hired online test

#!/usr/bin/env python3
# coding: utf-8

"""
Challenge 4: Deviation

Given an array of integer elements and an integer d please consider all the
sequences of d consecutive 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)

Note that your function will receive the following arguments:

- v, which is the array of integers
- d, which is an integer value giving the length of the sequences

Data constraints

- the array will contain up to 100,000 elements
- all the elements in the array are integer numbers in the following range:
[1, 2^31 - 1]
- the value of d will not exceed the length of the given array

Efficiency constraints

- your function is expected to print the result in less than 2 seconds

Example Input

v: 6, 9, 4, 7, 4, 1
d: 3

Example Output

6

Explanation

The sequences of length 3 are:
- 6 9 4 having the median 5 (the minimum value in the sequence is 4 and
the maximum is 9)
- 9 4 7 having the median 5 (the minimum value in the sequence is 4 and
the maximum is 9)
- 7 4 1 having the median 6 (the minimum value in the sequence is 1 and
the maximum is 7)
The maximum value among all medians is 6
"""

def find_deviation(v, d):
# Write your code here
# To print results to the standard output you can use print
# Example print "Hello world!"


大意:给定一个数组arr[],找出长度为d的连续子数组的最大值最小值差值中的最大值

思路:单调队列实现sliding window,得到每个长度为d的连续数组的最大值和最小值。

因为每个元素均进出一次单调队列,因此时间复杂度为O(n),队列最多保存d个元素的下标,因此空间复杂度为O(d)。

#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

int findDeviation(int arr[], int n, int d)
{
if(d < 2 || n < 2) return 0;

d = min(n, d);
queue<int> incQueue, decQueue;
//initialize
incQueue.push(0);
decQueue.push(0);
for(int i = 1; i < d; ++i){
//incQueue keep record of the minimum element within sub array of length d
while(!incQueue.empty() && arr[incQueue.front()] >= arr[i]) incQueue.pop();
incQueue.push(i);
//decQueue keep record of the maximum element within sub array of length d
while(!decQueue.empty() && arr[decQueue.front()] <= arr[i]) decQueue.pop();
decQueue.push(i);
}
/*
printf("in arr[%d,%d), M = %d, m = %d\n",
0, d, arr[decQueue.front()], arr[incQueue.front()]);
*/
//traverse till all elements
int res = arr[decQueue.front()] - arr[incQueue.front()];
for(int i = d; i < n; ++i){
//pop out invalid elements of this d-length subarray
if(incQueue.front() + d <= i) incQueue.pop();
while(!incQueue.empty() && arr[incQueue.front()] >= arr[i]) incQueue.pop();
incQueue.push(i);
//perform the same operation to decQueue
if(decQueue.front() + d <= i) decQueue.pop();
while(!decQueue.empty() && arr[decQueue.front()] <= arr[i]) decQueue.pop();
decQueue.push(i);
/*
printf("in arr[%d,%d), M = %d, m = %d\n",
i-d+1, i, arr[decQueue.front()], arr[incQueue.front()]);
*/
//update maximum deviation
res = max(res, arr[decQueue.front()] - arr[incQueue.front()]);
}
return res;
}

int main()
{
int arr[] = {6, 9, 4, 7, 4, 1}, d = 3;
printf("%d\n", findDeviation(arr, sizeof(arr)/sizeof(arr[0]), d));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: