Skip to content

[Prefix Sum] Matrix Block Sum #57

@sudosf

Description

@sudosf

Problem

Given an m x n matrix and integer k, return a matrix answer where answer[i][j] is the sum of all elements in the rectangle defined by (i-k, j-k) to (i+k, j+k), clamped to matrix boundaries.

References

Difficulty

🟡 Medium

Companies

Amazon, Google, Flipkart

Notes

Language: Java
2D prefix sum. prefix[i][j] = sum of all elements in rectangle (0,0) to (i-1,j-1).
Rectangle sum formula: prefix[r2+1][c2+1] - prefix[r1][c2+1] - prefix[r2+1][c1] + prefix[r1][c1].

Metadata

Metadata

Assignees

No one assigned

    Labels

    arraysArray problemsmediumMedium difficultyprefix-sumPrefix sum pattern

    Projects

    Status
    Backlog

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions