Grouped Cumulative Sum
MediumData EngineeringArraySortingPrefix Sum
Description
Given an array of [key, value] pairs, return an array aligned to the input where each position holds the running sum of values for that key up to and including that row.
Examples
Input:
[[1,10],[2,5],[1,20]]Output:
[10,5,30]Explanation:
Each row shows the accumulated total for its own key, summed in the order the rows arrive.
Input:
[[1,1],[1,2],[1,3]]Output:
[1,3,6]Explanation:
Each row shows the accumulated total for its own key, summed in the order the rows arrive.
Input:
[[5,5]]Output:
[5]Explanation:
Each row shows the accumulated total for its own key, summed in the order the rows arrive.
Constraints
- •
1 ≤ pairs ≤ 10⁴