Common Algorithm Implementation Templates
Traverse an array by groups
1 | for (int i = 0; i < arr.length; i += interval) { |
Calculate the submatrix sums fast
We can use a 2D prefix sum array. Each row of the 2D prefix sum array is a 1D prefix sum array for the corresponding row in the original matrix.
After initialization which takes $O(mn)$, calculating a submatrix sum takes $O(k)$, where k is the number of rows in the submatrix.
1 | // given matrix[m][n] |
Common Algorithm Implementation Templates
http://yenotes.org/2022/01/05/Common-Algorithm-Implementation-Templates/