-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1314-matrix-block-sum.js
45 lines (39 loc) · 1.29 KB
/
1314-matrix-block-sum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* 1314. Matrix Block Sum
* https://leetcode.com/problems/matrix-block-sum/
* Difficulty: Medium
*
* Given a m x n matrix mat and an integer k, return a matrix answer where each answer[i][j]
* is the sum of all elements mat[r][c] for:
* - i - k <= r <= i + k,
* - j - k <= c <= j + k, and
* - (r, c) is a valid position in the matrix.
*/
/**
* @param {number[][]} mat
* @param {number} k
* @return {number[][]}
*/
var matrixBlockSum = function(mat, k) {
const rows = mat.length;
const cols = mat[0].length;
const prefixSums = new Array(rows + 1).fill().map(() => new Array(cols + 1).fill(0));
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
prefixSums[i + 1][j + 1] = prefixSums[i + 1][j]
+ prefixSums[i][j + 1] - prefixSums[i][j] + mat[i][j];
}
}
const result = new Array(rows).fill().map(() => new Array(cols).fill(0));
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const top = Math.max(0, i - k);
const bottom = Math.min(rows - 1, i + k);
const left = Math.max(0, j - k);
const right = Math.min(cols - 1, j + k);
result[i][j] = prefixSums[bottom + 1][right + 1]
- prefixSums[bottom + 1][left] - prefixSums[top][right + 1] + prefixSums[top][left];
}
}
return result;
};