|
39 | 39 |
|
40 | 40 | ## Solutions
|
41 | 41 |
|
42 |
| -### Solution 1 |
| 42 | +### Solution 1: Two-Dimensional Prefix Sum |
| 43 | + |
| 44 | +The problem is actually asking for the number of prefix submatrices in a two-dimensional matrix whose sum is less than or equal to $k$. |
| 45 | + |
| 46 | +The calculation formula for the two-dimensional prefix sum is: |
| 47 | + |
| 48 | +$$ |
| 49 | +s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + x |
| 50 | +$$ |
| 51 | + |
| 52 | +The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. |
43 | 53 |
|
44 | 54 | <!-- tabs:start -->
|
45 | 55 |
|
46 | 56 | ```python
|
47 |
| - |
| 57 | +class Solution: |
| 58 | + def countSubmatrices(self, grid: List[List[int]], k: int) -> int: |
| 59 | + s = [[0] * (len(grid[0]) + 1) for _ in range(len(grid) + 1)] |
| 60 | + ans = 0 |
| 61 | + for i, row in enumerate(grid, 1): |
| 62 | + for j, x in enumerate(row, 1): |
| 63 | + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + x |
| 64 | + ans += s[i][j] <= k |
| 65 | + return ans |
48 | 66 | ```
|
49 | 67 |
|
50 | 68 | ```java
|
51 |
| - |
| 69 | +class Solution { |
| 70 | + public int countSubmatrices(int[][] grid, int k) { |
| 71 | + int m = grid.length, n = grid[0].length; |
| 72 | + int[][] s = new int[m + 1][n + 1]; |
| 73 | + int ans = 0; |
| 74 | + for (int i = 1; i <= m; ++i) { |
| 75 | + for (int j = 1; j <= n; ++j) { |
| 76 | + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; |
| 77 | + if (s[i][j] <= k) { |
| 78 | + ++ans; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return ans; |
| 83 | + } |
| 84 | +} |
52 | 85 | ```
|
53 | 86 |
|
54 | 87 | ```cpp
|
55 |
| - |
| 88 | +class Solution { |
| 89 | +public: |
| 90 | + int countSubmatrices(vector<vector<int>>& grid, int k) { |
| 91 | + int m = grid.size(), n = grid[0].size(); |
| 92 | + int s[m + 1][n + 1]; |
| 93 | + memset(s, 0, sizeof(s)); |
| 94 | + int ans = 0; |
| 95 | + for (int i = 1; i <= m; ++i) { |
| 96 | + for (int j = 1; j <= n; ++j) { |
| 97 | + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; |
| 98 | + if (s[i][j] <= k) { |
| 99 | + ++ans; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + return ans; |
| 104 | + } |
| 105 | +}; |
56 | 106 | ```
|
57 | 107 |
|
58 | 108 | ```go
|
| 109 | +func countSubmatrices(grid [][]int, k int) (ans int) { |
| 110 | + s := make([][]int, len(grid)+1) |
| 111 | + for i := range s { |
| 112 | + s[i] = make([]int, len(grid[0])+1) |
| 113 | + } |
| 114 | + for i, row := range grid { |
| 115 | + for j, x := range row { |
| 116 | + s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + x |
| 117 | + if s[i+1][j+1] <= k { |
| 118 | + ans++ |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + return |
| 123 | +} |
| 124 | +``` |
59 | 125 |
|
| 126 | +```ts |
| 127 | +function countSubmatrices(grid: number[][], k: number): number { |
| 128 | + const m = grid.length; |
| 129 | + const n = grid[0].length; |
| 130 | + const s: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); |
| 131 | + let ans: number = 0; |
| 132 | + for (let i = 1; i <= m; ++i) { |
| 133 | + for (let j = 1; j <= n; ++j) { |
| 134 | + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; |
| 135 | + if (s[i][j] <= k) { |
| 136 | + ++ans; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + return ans; |
| 141 | +} |
60 | 142 | ```
|
61 | 143 |
|
62 | 144 | <!-- tabs:end -->
|
|
0 commit comments