Given a sequence of N numbers ,extract number of sequences of length K having range less than R?

心已入冬 提交于 2019-12-24 13:02:16

问题


There is an array of integers ,I have to find the number of sequences of K length having range (max - min of the subsequence) less than equal to R .Is there a relation between Number of sequences of length k and number of sequences of length K-1 ? I am trying to solve a practice question on SPOJ. I don't want the full solution,just point me in the right direction /suggestion/hint.

I was thinking of a deque like structure to maintain min and max elements of the array upto a certain index.However,when k is closer to n ,this would become close to o(n*n) which is too slow ,I am ideally looking at O(n) solution or O(n * log n) solution. It would be best if I can calculate the required value for K=1 to K=N using a recursion/iteration relation as the same answer maybe required again


回答1:


This is a perfect application for a deque. See my answer here.

You should be able to adapt that for your needs with almost no changes, giving you an O(N) solution.



来源:https://stackoverflow.com/questions/12882511/given-a-sequence-of-n-numbers-extract-number-of-sequences-of-length-k-having-ra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!