英文:
Algorithm for Cumulative Maximum with First Element Removal
问题
I am trying to implement a data structure for storing the cumulative maximum (cummax) list of a given list of integers with efficient remove-first-element operation. In a cumulative maximum list an element at index i
is determined as the maximum number among all elements in the given list with indices less than or equal to i
. For example, for the list [10, 7, 8, 0, 3, 11]
the cumulative maximum list should be [10, 10, 10, 10, 10, 11]
. Time complexity of constructing a cummax list is O(n)
by simply iterating over all the elements and storing the maximum element so far.
The issue is that I want removeFirst
operation to be more efficient than simply constructing the cummax list from scratch after it. For example, when removing the first element from the example list, the cummax list should become [7, 8, 8, 8, 11]
. After removing another one, it should be [8, 8, 8, 11]
.
Is it possible to make such a data structure with better time complexity (now it's O(n)
) for removing the first element?
英文:
I am trying to implement a data structure for storing the cumulative maximum (cummax) list of a given list of integers with efficient remove-first-element operation. In a cumulative maximum list an element at index i
is determined as the maximum number among all elements in the given list with indices less than or equal to i
. For example, for the list [10, 7, 8, 0, 3, 11]
the cumulative maximum list should be [10, 10, 10, 10, 10, 11]
. Time complexity of constructing a cummax list is O(n)
by simply iterating over all the elements and storing the maximum element so far.
The issue is that I want removeFirst
operation to be more efficient than simply constructing the cummax list from scratch after it. For example, when removing the first element from the example list, the cummax list should become [7, 8, 8, 8, 11]
. After removing another one, it should be [8, 8, 8, 11]
.
Is it possible to make such a data structure with better time complexity (now it's O(n)
) for removing the first element?
答案1
得分: 1
我们可以使用已知的“范围最小/最大查询”程序来得出一个一般性解决方案。对于位于索引 j
处且累积最大值的第一个元素(在左侧)位于索引 i
处的任何一个元素,值 cumulativeMax(j)
将与 rangeMaximumQuery(i, j)
相同。
英文:
We can have a general solution using known "range minimum/maximum query" procedures. For any one element at index j
with cumulative maximum first element (on the left) at index i
, the value cumulativeMax(j)
would be the same as rangeMaximumQuery(i, j)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论