
Problem Statement
In this problem, you are provided with an integer length
indicating the size of an array arr
which is initially filled with zeros. An array updates
is also given, wherein each element is a tuple of the form [startIdxi, endIdxi, inci]
. These tuples specify a range in the arr
(from startIdxi
to endIdxi
inclusive) where each element in this range should be incremented by inci
.
The task is to apply these increments to the arr
according to the specified ranges in updates
and return the modified arr
after all updates have been applied.
Examples
Example 1
Input:
length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]]
Output:
[-2,0,3,5,3]
Example 2
Input:
length = 10, updates = [[2,4,6],[5,6,8],[1,9,-4]]
Output:
[0,-4,2,2,2,4,4,-4,-4,-4]
Constraints
1 <= length <= 105
0 <= updates.length <= 104
0 <= startIdxi <= endIdxi < length
-1000 <= inci <= 1000
Approach and Intuition
To solve this problem optimally, we can employ the concept of a difference array along with prefix sums. Here's how this can be approached:
Initialize a list
arr
of lengthlength+1
to zero. This extra space is used to prevent index out of bounds and simplify calculations.For each update
[startIdxi, endIdxi, inci]
:- Increment
arr[startIdxi]
byinci
. This is becausearr[startIdxi]
and all elements after it should be increased byinci
. - Decrement
arr[endIdxi + 1]
byinci
(if within bounds). This will mark the end of increment operation beyond theendIdxi
position.
- Increment
Compute the prefix sums of the
arr
to translate these increment and decrement operations into actual values for each position in the original arrayarr[0:length-1]
.The resulting array after applying prefix sums will represent the expected outcome after all update operations are applied onto the initial array filled with zeros.
Using this differential approach converts the otherwise O(n*m) complexity (where n is the length of the array and m the number of updates) to a more efficient O(n + m) complexity, due to linear traversals for applying updates and computing prefix sums separately.
Solutions
- C++
vector<int> updateArray(int size, vector<vector<int> > modifications)
{
vector<int> modifiedArray(size, 0);
for (auto& update : modifications) {
int startIndex = update[0], endIndex = update[1], increment = update[2];
modifiedArray[startIndex] += increment;
if (endIndex < size - 1)
modifiedArray[endIndex + 1] -= increment;
}
partial_sum(modifiedArray.begin(), modifiedArray.end(), modifiedArray.begin());
return modifiedArray;
}
This C++ solution involves implementing the range addition technique efficiently, suitable for scenarios where frequent updates to a subsection of an array are needed. The function updateArray
takes an integer size
and a 2D vector modifications
as inputs. It initializes a vector modifiedArray
of length size
with all zeros.
For each update
parameter in modifications
:
- Retrieve the start and end indices along with the increment.
- Apply the increment at the start index.
- If not addressing the last element, decrement the value just after the end index to nullify the increment effect beyond this range.
After processing all updates, compute the cumulative sum of modifiedArray
to reflect all increments properly across specified ranges using the partial_sum
function. The result is the updated array after all modifications, which is returned by the function. This range addition approach helps in modifying the array in an optimized manner for multiple updates.
No comments yet.