如何找到在日志(n)的时间序列中任何范围的最大值?最大值、序列、范围、时间

由网友(逼着放弃最痛苦)分享简介:例如。阵列:{1,5,2,3,2,10} E.g. Array: {1, 5, 2, 3, 2, 10}范围:0-1答案:5范围:2-4答案:3范围:0-5答案:10等等。Range: 0-1 Answer: 5Range: 2-4 Answer: 3Range: 0-5 Answer: 10etc....

例如。阵列:{1,5,2,3,2,10}

E.g. Array: {1, 5, 2, 3, 2, 10}

范围:0-1答案:5 范围:2-4答案:3 范围:0-5答案:10 等等。

Range: 0-1 Answer: 5 Range: 2-4 Answer: 3 Range: 0-5 Answer: 10 etc.

推荐答案

如果阵列没有排序,那么有没有办法做到你的要求是什么。

If the array is not sorted, then there is no way to do what you're asking for.

要找到最大的,你至少需要的检查的范围内,这需要O的所有元素( N 的)。

To find the maximum you at least need to inspect all elements in the range, which takes O(n).

如果您允许某种形式的数据preprocessing,这很容易。你可以只建立一个n 2 查找表的答案。然后,你可以找到最大的在固定时间内的任何范围。

If you allow some form of preprocessing of the data, it's easy. You could just build an n2 lookup table with the answers. Then you could find the maximum for any range in constant time.

阅读全文

相关推荐

最新文章