Daily buckets
EasyOnline Assessment
Split a list of amounts into consecutive groups of a fixed size and return the sum of each group.
Take the first size entries of amounts as group 1, the next size as group 2, and so on, keeping the original order. The final group is shorter if the entries do not divide evenly; it is still summed and included.
Input: amounts, a list of integers, and size, a positive integer.
Output: a list of integers, one sum per group, in order. If amounts is empty, return [].
Example 1
- Input:
- [[2,5,1,8,4],2]
- Output:
- [7,9,4]
- Explanation
- With size 2 the groups are [2, 5], [1, 8] and [4], with sums 7, 9 and 4. The last group has only one entry.
Example 2
- Input:
- [[-2,2,9],8]
- Output:
- [9]
- Explanation
- Size 8 is larger than the list, so everything falls into one short group: -2 + 2 + 9 = 9.
Example 3
- Input:
- [[1,2,3,4,5,6],3]
- Output:
- [6,15]
- Explanation
- The groups are [1, 2, 3] and [4, 5, 6], summing to 6 and 15.
Constraints
0 <= amounts.length <= 10,000-1,000,000 <= amounts[i] <= 1,000,0001 <= size <= 1,000