找出子数组中最大值最小值差值的最大值
2014-12-02 21:39
239 查看
一个题目,据说来自Hired online test
大意:给定一个数组arr[],找出长度为d的连续子数组的最大值最小值差值中的最大值
思路:单调队列实现sliding window,得到每个长度为d的连续数组的最大值和最小值。
因为每个元素均进出一次单调队列,因此时间复杂度为O(n),队列最多保存d个元素的下标,因此空间复杂度为O(d)。
#!/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; }
相关文章推荐
- java__for找出数组中最大,最小值
- python_lintcode_698数组中最大的差值_3. 统计数字_12. 带最小值操作的栈
- 给你些火柴棍,找出能摆的最大大值和最小值
- 找出数组中依序的最大差值方法(非动态规划版)
- 输入一个数组,用main函数调用一个Lookfor函数找出最大值 最小值 运行结果错误 。请大家帮忙找一下原因!
- 算法导论第9章(o(3n/2)时间内找出最大最小值)
- 使用一个函数找出整型数组的最大值或最小值
- 长度为n的数组有正有负,找出元素之和最大的子数组
- python 找出list中差值最大的两个数
- 找出数组中两段不相交的子数组,使其差值最大
- 一个很容易被坑的点(找出最大最小值并交换)
- 一次循环找出最大值最小值
- JAVA冒泡排序/JAVA冒泡排序再找出给定数据中的最大值最小值/JAVA数组排序
- C++:输入10个数字,找出最大最小值(可处理误输入)
- 数组进阶找出最大或最小值
- apply提高数组找出最大最小值的方式(性能)
- 如果给定一个数组arr[0,...N-1],要求找出相邻两个数的最大差值
- 第九章中位数与顺序统计学之“同时找出最小值和最大值”
- Powershell小技巧之找出最大最小值
- 我的java之路——找出100个范围在0-99的随机数的最大值和最小值并统计大于50的整数的个数