Arrays
Prefix Sum
Problem Statement
Given an array of integers nums
and two indices i
and j
, write a function range_sum(nums, i, j)
that returns the sum of elements from index i
to index j
, inclusive. Assume 0 <= i <= j < len(nums)
.
Example:
In this example, the subarray from index 1
to 3
is [2, 3, 4]
, so the function should return 2 + 3 + 4 = 9
.
Solution Using Prefix Sum
To avoid recalculating the sum every time, we can use a prefix sum array. The prefix sum array stores the cumulative sum up to each index, allowing us to calculate the sum of any subarray in constant time.
Steps
Build a prefix sum array where
prefix[k]
holds the sum of elements from the start up to indexk-1
.For a range sum from
i
toj
, the result is given byprefix[j + 1] - prefix[i]
.
Python Code
Explanation
The
prefix
array will look like this fornums = [1, 2, 3, 4, 5]
:Here,
prefix[k]
gives the sum of the firstk
elements innums
.
For
i = 1
andj = 3
, the result is:
This approach improves efficiency when multiple range queries need to be handled on the same array. The prefix sum technique reduces the time complexity of each query to O(1)O(1)O(1) after an initial O(n)O(n)O(n) setup for the prefix array.
Prefix Max
Problem Statement
Given an array of integers nums
and an index i
, write a function max_up_to(nums, i)
that returns the maximum value from the start of the array up to index i
, inclusive. Assume 0 <= i < len(nums)
.
Example:
In this example, the subarray from index 0
to 4
is [3, 1, 4, 1, 5]
, so the function should return the maximum value in this range, which is 5
.
Solution Using Prefix Max
To efficiently retrieve the maximum up to any index, we can create a prefix max array. The prefix max array at each index will store the maximum value encountered from the start of the array up to that index.
Steps
Build a prefix max array where
prefix_max[k]
holds the maximum value from the start up to indexk
.For any given index
i
, simply returnprefix_max[i]
.
Python Code
Explanation
The
prefix_max
array will look like this fornums = [3, 1, 4, 1, 5, 9, 2]
:Each entry
prefix_max[k]
holds the maximum value fromnums[0]
up tonums[k]
.
For
i = 4
, the result isprefix_max[4]
, which is5
.
Why Use Prefix Max?
The prefix max technique is efficient when we need to query the maximum up to various indices multiple times. After building the prefix_max
array in O(n)O(n)O(n), each query can be answered in O(1)O(1)O(1). This is particularly useful for range maximum queries on a fixed array, such as tracking highest scores or temperatures over time up to a given day.
Suffix Max
Problem Statement
Given an array of integers nums
and an index i
, write a function max_from(nums, i)
that returns the maximum value from index i
to the end of the array, inclusive. Assume 0 <= i < len(nums)
.
Example:
In this example, the subarray from index 3
to the end is [1, 5, 9, 2]
, so the function should return the maximum value in this range, which is 9
.
Solution Using Suffix Max
To efficiently retrieve the maximum from any index to the end of the array, we can create a suffix max array. The suffix max array at each index will store the maximum value from that index up to the end of the array.
Steps
Build a suffix max array where
suffix_max[k]
holds the maximum value from indexk
to the end of the array.For any given index
i
, simply returnsuffix_max[i]
.
Python Code
Explanation
The
suffix_max
array will look like this fornums = [3, 1, 4, 1, 5, 9, 2]
:Each entry
suffix_max[k]
holds the maximum value fromnums[k]
to the end of the array.
For
i = 3
, the result issuffix_max[3]
, which is9
.
Why Use Suffix Max?
The suffix max technique is useful when you need to query the maximum from any given index to the end multiple times. After building the suffix_max
array in O(n)O(n)O(n), each query can be answered in O(1)O(1)O(1). This technique is often applied in scenarios such as tracking future maximum values in financial data, or solving problems where decisions depend on future values in an array.
Above concepts can be used to solve Rain water trapping problem
Last updated