Abstract
- Basically an Array same length of the given array
- The value at each index is the sum of values from index 0
- Efficiently calculates sums of ranges within an array by leveraging on Memoization
Property
O(1) Range Sum Query
- With one time computation of the Prefix Sum (前缀和) in
O(n)
, we can retrieve the sum of elements in a Contiguous Segment inO(1)
- Let be a prefix sum of a given array, sums up the element in between the and (inclusive) of the given array
- Thus, we can obtain the sum of any sub-sequence of the given array in
O(1)
with the following formula
Tip
is same as
Complexity
Time Complexity
O(n)
to calculate prefix sum array of n size array- O(1) Range Sum Query