Skip to content

Practice Matrix Block Sum

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1314
LeetCode Problem

Matrix Block Sum

Problem Statement

For every position (i, j) in the m by n matrix mat, sum the values at positions (r, c) satisfying i - k <= r <= i + k and j - k <= c <= j + k. Include only positions inside mat. Return the matrix of these sums.

Source: Matrix Block Sum on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
mat = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output
[[12,21,16],[27,45,33],[24,39,28]]

# Example 2

Input
mat = [[1,2,3],[4,5,6],[7,8,9]], k = 2
Output
[[45,45,45],[45,45,45],[45,45,45]]

Constraints

m == mat.length
n == mat[i].length
1 <= m, n, k <= 100
1 <= mat[i][j] <= 100

Related LeetCode Problems

Practice more problems similar to Matrix Block Sum.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.