Let's make this one less mysterious. For 1314. Matrix Block Sum, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming, two pointers.
The notes already sitting in the source point us in the right direction:
- Hint 3
- Create a cumulative sum matrix where dp[i][j] is the sum of all cells in the rectangle from (0,0) to (i,j),
- use inclusion-exclusion idea.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The solution is organized around the main LeetCode entry point and a few local helpers.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 28 ms, faster than 79.68% of C++ online submissions for Matrix Block Sum.
02//Memory Usage: 11.4 MB, less than 100.00% of C++ online submissions for Matrix Block Sum.
03
04//Hint 3
05//Create a cumulative sum matrix where dp[i][j] is the sum of all cells in the rectangle from (0,0) to (i,j),
06//use inclusion-exclusion idea.
07
08class Solution {
09public:
10 vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int K) {
11 int m = mat.size(), n = mat[0].size();
12 vector<vector<int>> dp = mat;
13 vector<vector<int>> ans;
14 int bottom, right, top, left;
15
16 for(int i = 0; i < m; i++){
17 for(int j = 0; j < n; j++){
18 if(i > 0){
19 dp[i][j] += dp[i-1][j];
20 }
21 if(j > 0){
22 dp[i][j] += dp[i][j-1];
23 }
24 if(i > 0 && j > 0){
25 dp[i][j] -= dp[i-1][j-1];
26 }
27 cout << dp[i][j] << " ";
28 }
29 cout << endl;
30 }
31
32 ans = dp;
33
34 for(int i = 0; i < m; i++){
35 for(int j = 0; j < n; j++){
36 bottom = min(i + K, m - 1);
37 right = min(j + K, n - 1);
38 top = max(i - K - 1, -1); //-1 means invalid
39 left = max(j - K - 1, -1); //-1 means invalid
40 ans[i][j] = dp[bottom][right];
41
42 if(top != -1){
43 ans[i][j] -= dp[top][right];
44 // cout << i << ", " << j << ":- " << dp[top][right] << endl;
45 }
46 if(left >= 0){
47 ans[i][j] -= dp[bottom][left];
48 // cout << i << ", " << j << ":- " << dp[bottom][left] << endl;
49 }
50 if(top >= 0 && left >= 0){
51 ans[i][j] += dp[top][left];
52 // cout << i << ", " << j << ":+ " << dp[top][left] << endl;
53 }
54 }
55 }
56
57 return ans;
58 }
59};
Cost