累积最大值算法,首个元素移除。

huangapple go评论65阅读模式
英文:

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).

huangapple
  • 本文由 发表于 2023年7月6日 19:52:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定