From 207d9d3635037f7c62d6ac9dc0598d24f4b75bd2 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 13 Dec 2023 08:45:14 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2132 No.2132.Stamping the Grid --- .../2132.Stamping the Grid/README.md | 302 ++++++++++-------- .../2132.Stamping the Grid/README_EN.md | 302 ++++++++++-------- .../2132.Stamping the Grid/Solution.cpp | 66 ++-- .../2132.Stamping the Grid/Solution.go | 45 +-- .../2132.Stamping the Grid/Solution.java | 65 ++-- .../2132.Stamping the Grid/Solution.js | 39 ++- .../2132.Stamping the Grid/Solution.py | 52 ++- .../2132.Stamping the Grid/Solution.ts | 33 ++ 8 files changed, 495 insertions(+), 409 deletions(-) create mode 100644 solution/2100-2199/2132.Stamping the Grid/Solution.ts diff --git a/solution/2100-2199/2132.Stamping the Grid/README.md b/solution/2100-2199/2132.Stamping the Grid/README.md index 4be4817d47cc5..93ad113e8c7d0 100644 --- a/solution/2100-2199/2132.Stamping the Grid/README.md +++ b/solution/2100-2199/2132.Stamping the Grid/README.md @@ -60,21 +60,24 @@ **方法一:二维前缀和 + 二维差分** -根据题给的约束,很容易推出,一个格子能贴邮票的条件为,在加上邮票的长和宽后,右下角不越界的情况下,当前子区域中所有格子的和为 0。 +根据题目描述,每一个空格子都必须被邮票覆盖,而且不能覆盖任何被占据的格子。因此,我们可以遍历二维矩阵,对于每个格子,如果以该格子为左上角的 $stampHeight \times stampWidth$ 的区域内的所有格子都是空格子(即没有被占据),那么我们就可以在该格子处放置一个邮票。 -那么显然我们可以维护一个二维的前缀和数组,在 `O(1)` 的时间复杂度下就可以判断每次遍历到的格子是否能贴邮票。 +为了快速判断一个区域内的所有格子是否都是空格子,我们可以使用二维前缀和。我们用 $s_{i,j}$ 表示二维矩阵中从 $(1,1)$ 到 $(i,j)$ 的子矩阵中被占据的格子的数量。即 $s_{i, j} = s_{i - 1, j} + s_{i, j - 1} - s_{i - 1, j - 1} + grid_{i-1, j-1}$。 -而因为贴邮票的操作,可以概括为将当前子区域的所有格子的值都置为 `1`,很自然的就能想到用一个二维的差分数组来维护贴邮票后的状态。 +那么以 $(i, j)$ 为左上角,且高度和宽度分别为 $stampHeight$ 和 $stampWidth$ 的子矩阵的右下角坐标为 $(x, y) = (i + stampHeight - 1, j + stampWidth - 1)$,我们可以通过 $s_{x, y} - s_{x, j - 1} - s_{i - 1, y} + s_{i - 1, j - 1}$ 来计算出该子矩阵中被占据的格子的数量。如果该子矩阵中被占据的格子的数量为 $0$,那么我们就可以在 $(i, j)$ 处放置一个邮票,放置邮票后,这 $stampHeight \times stampWidth$ 的区域内的所有格子都会变成被占据的格子,我们可以用二维差分数组 $d$ 来记录这一变化。即: -最后只要对该差分数组再求一次二维前缀和,只要当前格子的和为 `0`,就意味着存在没有覆盖完全的情况,直接返回 `false` 即可。 +$$ +\begin{aligned} +d_{i, j} &\leftarrow d_{i, j} + 1 \\ +d_{i, y + 1} &\leftarrow d_{i, y + 1} - 1 \\ +d_{x + 1, j} &\leftarrow d_{x + 1, j} - 1 \\ +d_{x + 1, y + 1} &\leftarrow d_{x + 1, y + 1} + 1 +\end{aligned} +$$ -需要注意的是二维数组的下标关系,具体参考如下。 +最后,我们对二维差分数组 $d$ 进行前缀和运算,可以得出每个格子被邮票覆盖的次数。如果某个格子没有被占据,且被邮票覆盖的次数为 $0$,那么我们就无法将邮票放置在该格子处,因此我们需要返回 $\texttt{false}$。如果所有“没被占据的格子”都成功被邮票覆盖,返回 $\texttt{true}$。 -`s[i + 1][j + 1]` 表示第 i 行第 j 列左上部分所有元素之和,其中 i, j 下标从 0 开始。 - -则 `s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + nums[i][j]`。 - -以 (x1, y1) 为左上角,(x2, y2) 为右下角的子矩阵和 `sub = s[x2 + 1][y2 + 1] - s[x2 + 1][y1] - s[x1][y2 + 1] + s[x1][y1]`。 +时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是二维矩阵的高度和宽度。 @@ -89,26 +92,22 @@ class Solution: ) -> bool: m, n = len(grid), len(grid[0]) s = [[0] * (n + 1) for _ in range(m + 1)] - for i, row in enumerate(grid): - for j, v in enumerate(row): - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + v - - d = [[0] * (n + 1) for _ in range(m + 1)] - for i, row in enumerate(grid): - for j, v in enumerate(row): - if v == 0: - x, y = i + stampHeight, j + stampWidth - if x <= m and y <= n and s[x][y] - s[x][j] - s[i][y] + s[i][j] == 0: - d[i][j] += 1 - d[i][y] -= 1 - d[x][j] -= 1 - d[x][y] += 1 - - cnt = [[0] * (n + 1) for _ in range(m + 1)] - for i, row in enumerate(grid): - for j, v in enumerate(row): - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j] - if v == 0 and cnt[i + 1][j + 1] == 0: + for i, row in enumerate(grid, 1): + for j, v in enumerate(row, 1): + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + v + d = [[0] * (n + 2) for _ in range(m + 2)] + for i in range(1, m - stampHeight + 2): + for j in range(1, n - stampWidth + 2): + x, y = i + stampHeight - 1, j + stampWidth - 1 + if s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] == 0: + d[i][j] += 1 + d[i][y + 1] -= 1 + d[x + 1][j] -= 1 + d[x + 1][y + 1] += 1 + for i, row in enumerate(grid, 1): + for j, v in enumerate(row, 1): + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1] + if v == 0 and d[i][j] == 0: return False return True ``` @@ -122,30 +121,27 @@ class Solution { public boolean possibleToStamp(int[][] grid, int stampHeight, int stampWidth) { int m = grid.length, n = grid[0].length; int[][] s = new int[m + 1][n + 1]; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j]; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; } } - int[][] d = new int[m + 1][n + 1]; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - if (grid[i][j] == 0) { - int x = i + stampHeight, y = j + stampWidth; - if (x <= m && y <= n && s[x][y] - s[x][j] - s[i][y] + s[i][j] == 0) { - d[i][j]++; - d[i][y]--; - d[x][j]--; - d[x][y]++; - } + int[][] d = new int[m + 2][n + 2]; + for (int i = 1; i + stampHeight - 1 <= m; ++i) { + for (int j = 1; j + stampWidth - 1 <= n; ++j) { + int x = i + stampHeight - 1, y = j + stampWidth - 1; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] == 0) { + d[i][j]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; } } } - int[][] cnt = new int[m + 1][n + 1]; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j]; - if (grid[i][j] == 0 && cnt[i + 1][j + 1] == 0) { + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] == 0 && d[i][j] == 0) { return false; } } @@ -163,29 +159,31 @@ public: bool possibleToStamp(vector>& grid, int stampHeight, int stampWidth) { int m = grid.size(), n = grid[0].size(); vector> s(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j]; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; } } - vector> d(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - if (grid[i][j]) continue; - int x = i + stampHeight, y = j + stampWidth; - if (x <= m && y <= n && s[x][y] - s[i][y] - s[x][j] + s[i][j] == 0) { + + vector> d(m + 2, vector(n + 2)); + for (int i = 1; i + stampHeight - 1 <= m; ++i) { + for (int j = 1; j + stampWidth - 1 <= n; ++j) { + int x = i + stampHeight - 1, y = j + stampWidth - 1; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] == 0) { d[i][j]++; - d[x][j]--; - d[i][y]--; - d[x][y]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; } } } - vector> cnt(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j]; - if (grid[i][j] == 0 && cnt[i + 1][j + 1] == 0) return false; + + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] == 0 && d[i][j] == 0) { + return false; + } } } return true; @@ -193,6 +191,88 @@ public: }; ``` +### **Go** + +```go +func possibleToStamp(grid [][]int, stampHeight int, stampWidth int) bool { + m, n := len(grid), len(grid[0]) + s := make([][]int, m+1) + for i := range s { + s[i] = make([]int, n+1) + } + for i := 1; i <= m; i++ { + for j := 1; j <= n; j++ { + s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + grid[i-1][j-1] + } + } + + d := make([][]int, m+2) + for i := range d { + d[i] = make([]int, n+2) + } + + for i := 1; i+stampHeight-1 <= m; i++ { + for j := 1; j+stampWidth-1 <= n; j++ { + x, y := i+stampHeight-1, j+stampWidth-1 + if s[x][y]-s[x][j-1]-s[i-1][y]+s[i-1][j-1] == 0 { + d[i][j]++ + d[i][y+1]-- + d[x+1][j]-- + d[x+1][y+1]++ + } + } + } + + for i := 1; i <= m; i++ { + for j := 1; j <= n; j++ { + d[i][j] += d[i-1][j] + d[i][j-1] - d[i-1][j-1] + if grid[i-1][j-1] == 0 && d[i][j] == 0 { + return false + } + } + } + return true +} +``` + +### **TypeScript** + +```ts +function possibleToStamp(grid: number[][], stampHeight: number, stampWidth: number): boolean { + const m = grid.length; + const n = grid[0].length; + const s: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + } + } + + const d: number[][] = Array.from({ length: m + 2 }, () => Array(n + 2).fill(0)); + for (let i = 1; i + stampHeight - 1 <= m; ++i) { + for (let j = 1; j + stampWidth - 1 <= n; ++j) { + const [x, y] = [i + stampHeight - 1, j + stampWidth - 1]; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] === 0) { + d[i][j]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; + } + } + } + + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] === 0 && d[i][j] === 0) { + return false; + } + } + } + return true; +} +``` + ### **Rust** ```rust @@ -264,49 +344,6 @@ impl Solution { } ``` -### **Go** - -```go -func possibleToStamp(grid [][]int, stampHeight int, stampWidth int) bool { - m, n := len(grid), len(grid[0]) - s := make([][]int, m+1) - d := make([][]int, m+1) - cnt := make([][]int, m+1) - for i := range s { - s[i] = make([]int, n+1) - d[i] = make([]int, n+1) - cnt[i] = make([]int, n+1) - } - for i, row := range grid { - for j, v := range row { - s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + v - } - } - for i, row := range grid { - for j, v := range row { - if v == 0 { - x, y := i+stampHeight, j+stampWidth - if x <= m && y <= n && s[x][y]-s[i][y]-s[x][j]+s[i][j] == 0 { - d[i][j]++ - d[i][y]-- - d[x][j]-- - d[x][y]++ - } - } - } - } - for i, row := range grid { - for j, v := range row { - cnt[i+1][j+1] = cnt[i+1][j] + cnt[i][j+1] - cnt[i][j] + d[i][j] - if v == 0 && cnt[i+1][j+1] == 0 { - return false - } - } - } - return true -} -``` - ### **JavaScript** ```js @@ -319,31 +356,30 @@ func possibleToStamp(grid [][]int, stampHeight int, stampWidth int) bool { var possibleToStamp = function (grid, stampHeight, stampWidth) { const m = grid.length; const n = grid[0].length; - let s = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0)); - let d = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0)); - let cnt = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0)); - for (let i = 0; i < m; ++i) { - for (let j = 0; j < n; ++j) { - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j]; + const s = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; } } - for (let i = 0; i < m; ++i) { - for (let j = 0; j < n; ++j) { - if (grid[i][j] == 0) { - let [x, y] = [i + stampHeight, j + stampWidth]; - if (x <= m && y <= n && s[x][y] - s[i][y] - s[x][j] + s[i][j] == 0) { - d[i][j]++; - d[i][y]--; - d[x][j]--; - d[x][y]++; - } + + const d = Array.from({ length: m + 2 }, () => Array(n + 2).fill(0)); + for (let i = 1; i + stampHeight - 1 <= m; ++i) { + for (let j = 1; j + stampWidth - 1 <= n; ++j) { + const [x, y] = [i + stampHeight - 1, j + stampWidth - 1]; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] === 0) { + d[i][j]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; } } } - for (let i = 0; i < m; ++i) { - for (let j = 0; j < n; ++j) { - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j]; - if (grid[i][j] == 0 && cnt[i + 1][j + 1] == 0) { + + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] === 0 && d[i][j] === 0) { return false; } } @@ -352,14 +388,6 @@ var possibleToStamp = function (grid, stampHeight, stampWidth) { }; ``` -### **TypeScript** - - - -```ts - -``` - ### **...** ``` diff --git a/solution/2100-2199/2132.Stamping the Grid/README_EN.md b/solution/2100-2199/2132.Stamping the Grid/README_EN.md index 6994fff8c2c5b..af503fa927873 100644 --- a/solution/2100-2199/2132.Stamping the Grid/README_EN.md +++ b/solution/2100-2199/2132.Stamping the Grid/README_EN.md @@ -50,23 +50,26 @@ ## Solutions -Based on the constraints provided by the question, it's easy to infer that a cell can accommodate a stamp if, after adding the length and width of the stamp, the bottom-right corner does not exceed the boundary, and the sum of all cells in the current sub-area is zero. +**Solution 1: Two-Dimensional Prefix Sum + Two-Dimensional Difference** -Apparently, we can maintain a two-dimensional prefix sum array, and in `O(1)` time complexity, we can judge whether each cell traversed can accommodate a stamp. +According to the problem description, every empty cell must be covered by a stamp, and no occupied cell can be covered. Therefore, we can traverse the two-dimensional matrix, and for each cell, if all cells in the area of $stampHeight \times stampWidth$ with this cell as the upper left corner are empty (i.e., not occupied), then we can place a stamp at this cell. -Since the action of affixing a stamp can be generalized to setting the values of all cells in the current sub-area to `1`, it's natural to think of maintaining the state after stamp affixing using a two-dimensional difference array. +To quickly determine whether all cells in an area are empty, we can use a two-dimensional prefix sum. We use $s_{i,j}$ to represent the number of occupied cells in the sub-matrix from $(1,1)$ to $(i,j)$ in the two-dimensional matrix. That is, $s_{i, j} = s_{i - 1, j} + s_{i, j - 1} - s_{i - 1, j - 1} + grid_{i-1, j-1}$. -Finally, just calculate the two-dimensional prefix sum for this difference array again. +Then, with $(i, j)$ as the upper left corner, and the height and width are $stampHeight$ and $stampWidth$ respectively, the lower right coordinate of the sub-matrix is $(x, y) = (i + stampHeight - 1, j + stampWidth - 1)$. We can calculate the number of occupied cells in this sub-matrix through $s_{x, y} - s_{x, j - 1} - s_{i - 1, y} + s_{i - 1, j - 1}$. If the number of occupied cells in this sub-matrix is $0$, then we can place a stamp at $(i, j)$. After placing the stamp, all cells in this $stampHeight \times stampWidth$ area will become occupied cells. We can use a two-dimensional difference array $d$ to record this change. That is: -If the sum of the current cell is `0`, it means there are cases that have not been completely covered, and you can directly return `false`. +$$ +\begin{aligned} +d_{i, j} &\leftarrow d_{i, j} + 1 \\ +d_{i, y + 1} &\leftarrow d_{i, y + 1} - 1 \\ +d_{x + 1, j} &\leftarrow d_{x + 1, j} - 1 \\ +d_{x + 1, y + 1} &\leftarrow d_{x + 1, y + 1} + 1 +\end{aligned} +$$ -It's worth noting the subscript relationship of the two-dimensional array, which is as follows. +Finally, we perform a prefix sum operation on the two-dimensional difference array $d$ to find out the number of times each cell is covered by a stamp. If a cell is not occupied and the number of times it is covered by a stamp is $0$, then we cannot place a stamp at this cell, so we need to return $\texttt{false}$. If all "unoccupied cells" are successfully covered by stamps, return $\texttt{true}$. -`s[i + 1][j + 1]` represents the sum of all elements in the upper left part of the i-th row and j-th column, where the subscript i, j starts from 0. - -So `s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + nums[i][j]`. - -For a sub-matrix with (x1, y1) as the upper left corner and (x2, y2) as the bottom right corner, the sum `sub = s[x2 + 1][y2 + 1] - s[x2 + 1][y1] - s[x1][y2 + 1] + s[x1][y1]`. +The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the height and width of the two-dimensional matrix, respectively. @@ -79,26 +82,22 @@ class Solution: ) -> bool: m, n = len(grid), len(grid[0]) s = [[0] * (n + 1) for _ in range(m + 1)] - for i, row in enumerate(grid): - for j, v in enumerate(row): - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + v - - d = [[0] * (n + 1) for _ in range(m + 1)] - for i, row in enumerate(grid): - for j, v in enumerate(row): - if v == 0: - x, y = i + stampHeight, j + stampWidth - if x <= m and y <= n and s[x][y] - s[x][j] - s[i][y] + s[i][j] == 0: - d[i][j] += 1 - d[i][y] -= 1 - d[x][j] -= 1 - d[x][y] += 1 - - cnt = [[0] * (n + 1) for _ in range(m + 1)] - for i, row in enumerate(grid): - for j, v in enumerate(row): - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j] - if v == 0 and cnt[i + 1][j + 1] == 0: + for i, row in enumerate(grid, 1): + for j, v in enumerate(row, 1): + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + v + d = [[0] * (n + 2) for _ in range(m + 2)] + for i in range(1, m - stampHeight + 2): + for j in range(1, n - stampWidth + 2): + x, y = i + stampHeight - 1, j + stampWidth - 1 + if s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] == 0: + d[i][j] += 1 + d[i][y + 1] -= 1 + d[x + 1][j] -= 1 + d[x + 1][y + 1] += 1 + for i, row in enumerate(grid, 1): + for j, v in enumerate(row, 1): + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1] + if v == 0 and d[i][j] == 0: return False return True ``` @@ -110,30 +109,27 @@ class Solution { public boolean possibleToStamp(int[][] grid, int stampHeight, int stampWidth) { int m = grid.length, n = grid[0].length; int[][] s = new int[m + 1][n + 1]; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j]; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; } } - int[][] d = new int[m + 1][n + 1]; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - if (grid[i][j] == 0) { - int x = i + stampHeight, y = j + stampWidth; - if (x <= m && y <= n && s[x][y] - s[x][j] - s[i][y] + s[i][j] == 0) { - d[i][j]++; - d[i][y]--; - d[x][j]--; - d[x][y]++; - } + int[][] d = new int[m + 2][n + 2]; + for (int i = 1; i + stampHeight - 1 <= m; ++i) { + for (int j = 1; j + stampWidth - 1 <= n; ++j) { + int x = i + stampHeight - 1, y = j + stampWidth - 1; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] == 0) { + d[i][j]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; } } } - int[][] cnt = new int[m + 1][n + 1]; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j]; - if (grid[i][j] == 0 && cnt[i + 1][j + 1] == 0) { + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] == 0 && d[i][j] == 0) { return false; } } @@ -151,29 +147,31 @@ public: bool possibleToStamp(vector>& grid, int stampHeight, int stampWidth) { int m = grid.size(), n = grid[0].size(); vector> s(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j]; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; } } - vector> d(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - if (grid[i][j]) continue; - int x = i + stampHeight, y = j + stampWidth; - if (x <= m && y <= n && s[x][y] - s[i][y] - s[x][j] + s[i][j] == 0) { + + vector> d(m + 2, vector(n + 2)); + for (int i = 1; i + stampHeight - 1 <= m; ++i) { + for (int j = 1; j + stampWidth - 1 <= n; ++j) { + int x = i + stampHeight - 1, y = j + stampWidth - 1; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] == 0) { d[i][j]++; - d[x][j]--; - d[i][y]--; - d[x][y]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; } } } - vector> cnt(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j]; - if (grid[i][j] == 0 && cnt[i + 1][j + 1] == 0) return false; + + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] == 0 && d[i][j] == 0) { + return false; + } } } return true; @@ -181,6 +179,88 @@ public: }; ``` +### **Go** + +```go +func possibleToStamp(grid [][]int, stampHeight int, stampWidth int) bool { + m, n := len(grid), len(grid[0]) + s := make([][]int, m+1) + for i := range s { + s[i] = make([]int, n+1) + } + for i := 1; i <= m; i++ { + for j := 1; j <= n; j++ { + s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + grid[i-1][j-1] + } + } + + d := make([][]int, m+2) + for i := range d { + d[i] = make([]int, n+2) + } + + for i := 1; i+stampHeight-1 <= m; i++ { + for j := 1; j+stampWidth-1 <= n; j++ { + x, y := i+stampHeight-1, j+stampWidth-1 + if s[x][y]-s[x][j-1]-s[i-1][y]+s[i-1][j-1] == 0 { + d[i][j]++ + d[i][y+1]-- + d[x+1][j]-- + d[x+1][y+1]++ + } + } + } + + for i := 1; i <= m; i++ { + for j := 1; j <= n; j++ { + d[i][j] += d[i-1][j] + d[i][j-1] - d[i-1][j-1] + if grid[i-1][j-1] == 0 && d[i][j] == 0 { + return false + } + } + } + return true +} +``` + +### **TypeScript** + +```ts +function possibleToStamp(grid: number[][], stampHeight: number, stampWidth: number): boolean { + const m = grid.length; + const n = grid[0].length; + const s: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + } + } + + const d: number[][] = Array.from({ length: m + 2 }, () => Array(n + 2).fill(0)); + for (let i = 1; i + stampHeight - 1 <= m; ++i) { + for (let j = 1; j + stampWidth - 1 <= n; ++j) { + const [x, y] = [i + stampHeight - 1, j + stampWidth - 1]; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] === 0) { + d[i][j]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; + } + } + } + + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] === 0 && d[i][j] === 0) { + return false; + } + } + } + return true; +} +``` + ### **Rust** ```rust @@ -252,49 +332,6 @@ impl Solution { } ``` -### **Go** - -```go -func possibleToStamp(grid [][]int, stampHeight int, stampWidth int) bool { - m, n := len(grid), len(grid[0]) - s := make([][]int, m+1) - d := make([][]int, m+1) - cnt := make([][]int, m+1) - for i := range s { - s[i] = make([]int, n+1) - d[i] = make([]int, n+1) - cnt[i] = make([]int, n+1) - } - for i, row := range grid { - for j, v := range row { - s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + v - } - } - for i, row := range grid { - for j, v := range row { - if v == 0 { - x, y := i+stampHeight, j+stampWidth - if x <= m && y <= n && s[x][y]-s[i][y]-s[x][j]+s[i][j] == 0 { - d[i][j]++ - d[i][y]-- - d[x][j]-- - d[x][y]++ - } - } - } - } - for i, row := range grid { - for j, v := range row { - cnt[i+1][j+1] = cnt[i+1][j] + cnt[i][j+1] - cnt[i][j] + d[i][j] - if v == 0 && cnt[i+1][j+1] == 0 { - return false - } - } - } - return true -} -``` - ### **JavaScript** ```js @@ -307,31 +344,30 @@ func possibleToStamp(grid [][]int, stampHeight int, stampWidth int) bool { var possibleToStamp = function (grid, stampHeight, stampWidth) { const m = grid.length; const n = grid[0].length; - let s = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0)); - let d = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0)); - let cnt = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0)); - for (let i = 0; i < m; ++i) { - for (let j = 0; j < n; ++j) { - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j]; + const s = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; } } - for (let i = 0; i < m; ++i) { - for (let j = 0; j < n; ++j) { - if (grid[i][j] == 0) { - let [x, y] = [i + stampHeight, j + stampWidth]; - if (x <= m && y <= n && s[x][y] - s[i][y] - s[x][j] + s[i][j] == 0) { - d[i][j]++; - d[i][y]--; - d[x][j]--; - d[x][y]++; - } + + const d = Array.from({ length: m + 2 }, () => Array(n + 2).fill(0)); + for (let i = 1; i + stampHeight - 1 <= m; ++i) { + for (let j = 1; j + stampWidth - 1 <= n; ++j) { + const [x, y] = [i + stampHeight - 1, j + stampWidth - 1]; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] === 0) { + d[i][j]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; } } } - for (let i = 0; i < m; ++i) { - for (let j = 0; j < n; ++j) { - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j]; - if (grid[i][j] == 0 && cnt[i + 1][j + 1] == 0) { + + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] === 0 && d[i][j] === 0) { return false; } } @@ -340,12 +376,6 @@ var possibleToStamp = function (grid, stampHeight, stampWidth) { }; ``` -### **TypeScript** - -```ts - -``` - ### **...** ``` diff --git a/solution/2100-2199/2132.Stamping the Grid/Solution.cpp b/solution/2100-2199/2132.Stamping the Grid/Solution.cpp index 87b22ad8cc669..5fe5172a0871e 100644 --- a/solution/2100-2199/2132.Stamping the Grid/Solution.cpp +++ b/solution/2100-2199/2132.Stamping the Grid/Solution.cpp @@ -1,33 +1,35 @@ -class Solution { -public: - bool possibleToStamp(vector>& grid, int stampHeight, int stampWidth) { - int m = grid.size(), n = grid[0].size(); - vector> s(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j]; - } - } - vector> d(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - if (grid[i][j]) continue; - int x = i + stampHeight, y = j + stampWidth; - if (x <= m && y <= n && s[x][y] - s[i][y] - s[x][j] + s[i][j] == 0) { - d[i][j]++; - d[x][j]--; - d[i][y]--; - d[x][y]++; - } - } - } - vector> cnt(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j]; - if (grid[i][j] == 0 && cnt[i + 1][j + 1] == 0) return false; - } - } - return true; - } +class Solution { +public: + bool possibleToStamp(vector>& grid, int stampHeight, int stampWidth) { + int m = grid.size(), n = grid[0].size(); + vector> s(m + 1, vector(n + 1)); + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + } + } + + vector> d(m + 2, vector(n + 2)); + for (int i = 1; i + stampHeight - 1 <= m; ++i) { + for (int j = 1; j + stampWidth - 1 <= n; ++j) { + int x = i + stampHeight - 1, y = j + stampWidth - 1; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] == 0) { + d[i][j]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; + } + } + } + + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] == 0 && d[i][j] == 0) { + return false; + } + } + } + return true; + } }; \ No newline at end of file diff --git a/solution/2100-2199/2132.Stamping the Grid/Solution.go b/solution/2100-2199/2132.Stamping the Grid/Solution.go index cdf8fc435da80..0818652690c0d 100644 --- a/solution/2100-2199/2132.Stamping the Grid/Solution.go +++ b/solution/2100-2199/2132.Stamping the Grid/Solution.go @@ -1,38 +1,39 @@ func possibleToStamp(grid [][]int, stampHeight int, stampWidth int) bool { m, n := len(grid), len(grid[0]) s := make([][]int, m+1) - d := make([][]int, m+1) - cnt := make([][]int, m+1) for i := range s { s[i] = make([]int, n+1) - d[i] = make([]int, n+1) - cnt[i] = make([]int, n+1) } - for i, row := range grid { - for j, v := range row { - s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + v + for i := 1; i <= m; i++ { + for j := 1; j <= n; j++ { + s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + grid[i-1][j-1] } } - for i, row := range grid { - for j, v := range row { - if v == 0 { - x, y := i+stampHeight, j+stampWidth - if x <= m && y <= n && s[x][y]-s[i][y]-s[x][j]+s[i][j] == 0 { - d[i][j]++ - d[i][y]-- - d[x][j]-- - d[x][y]++ - } + + d := make([][]int, m+2) + for i := range d { + d[i] = make([]int, n+2) + } + + for i := 1; i+stampHeight-1 <= m; i++ { + for j := 1; j+stampWidth-1 <= n; j++ { + x, y := i+stampHeight-1, j+stampWidth-1 + if s[x][y]-s[x][j-1]-s[i-1][y]+s[i-1][j-1] == 0 { + d[i][j]++ + d[i][y+1]-- + d[x+1][j]-- + d[x+1][y+1]++ } } } - for i, row := range grid { - for j, v := range row { - cnt[i+1][j+1] = cnt[i+1][j] + cnt[i][j+1] - cnt[i][j] + d[i][j] - if v == 0 && cnt[i+1][j+1] == 0 { + + for i := 1; i <= m; i++ { + for j := 1; j <= n; j++ { + d[i][j] += d[i-1][j] + d[i][j-1] - d[i-1][j-1] + if grid[i-1][j-1] == 0 && d[i][j] == 0 { return false } } } return true -} \ No newline at end of file +} diff --git a/solution/2100-2199/2132.Stamping the Grid/Solution.java b/solution/2100-2199/2132.Stamping the Grid/Solution.java index 90563471af896..de13971a61a91 100644 --- a/solution/2100-2199/2132.Stamping the Grid/Solution.java +++ b/solution/2100-2199/2132.Stamping the Grid/Solution.java @@ -1,35 +1,32 @@ -class Solution { - public boolean possibleToStamp(int[][] grid, int stampHeight, int stampWidth) { - int m = grid.length, n = grid[0].length; - int[][] s = new int[m + 1][n + 1]; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j]; - } - } - int[][] d = new int[m + 1][n + 1]; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - if (grid[i][j] == 0) { - int x = i + stampHeight, y = j + stampWidth; - if (x <= m && y <= n && s[x][y] - s[x][j] - s[i][y] + s[i][j] == 0) { - d[i][j]++; - d[i][y]--; - d[x][j]--; - d[x][y]++; - } - } - } - } - int[][] cnt = new int[m + 1][n + 1]; - for (int i = 0; i < m; ++i) { - for (int j = 0; j < n; ++j) { - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j]; - if (grid[i][j] == 0 && cnt[i + 1][j + 1] == 0) { - return false; - } - } - } - return true; - } +class Solution { + public boolean possibleToStamp(int[][] grid, int stampHeight, int stampWidth) { + int m = grid.length, n = grid[0].length; + int[][] s = new int[m + 1][n + 1]; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + } + } + int[][] d = new int[m + 2][n + 2]; + for (int i = 1; i + stampHeight - 1 <= m; ++i) { + for (int j = 1; j + stampWidth - 1 <= n; ++j) { + int x = i + stampHeight - 1, y = j + stampWidth - 1; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] == 0) { + d[i][j]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; + } + } + } + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] == 0 && d[i][j] == 0) { + return false; + } + } + } + return true; + } } \ No newline at end of file diff --git a/solution/2100-2199/2132.Stamping the Grid/Solution.js b/solution/2100-2199/2132.Stamping the Grid/Solution.js index ca22ed42e2a02..7683c884de404 100644 --- a/solution/2100-2199/2132.Stamping the Grid/Solution.js +++ b/solution/2100-2199/2132.Stamping the Grid/Solution.js @@ -7,31 +7,30 @@ var possibleToStamp = function (grid, stampHeight, stampWidth) { const m = grid.length; const n = grid[0].length; - let s = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0)); - let d = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0)); - let cnt = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0)); - for (let i = 0; i < m; ++i) { - for (let j = 0; j < n; ++j) { - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j]; + const s = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; } } - for (let i = 0; i < m; ++i) { - for (let j = 0; j < n; ++j) { - if (grid[i][j] == 0) { - let [x, y] = [i + stampHeight, j + stampWidth]; - if (x <= m && y <= n && s[x][y] - s[i][y] - s[x][j] + s[i][j] == 0) { - d[i][j]++; - d[i][y]--; - d[x][j]--; - d[x][y]++; - } + + const d = Array.from({ length: m + 2 }, () => Array(n + 2).fill(0)); + for (let i = 1; i + stampHeight - 1 <= m; ++i) { + for (let j = 1; j + stampWidth - 1 <= n; ++j) { + const [x, y] = [i + stampHeight - 1, j + stampWidth - 1]; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] === 0) { + d[i][j]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; } } } - for (let i = 0; i < m; ++i) { - for (let j = 0; j < n; ++j) { - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j]; - if (grid[i][j] == 0 && cnt[i + 1][j + 1] == 0) { + + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] === 0 && d[i][j] === 0) { return false; } } diff --git a/solution/2100-2199/2132.Stamping the Grid/Solution.py b/solution/2100-2199/2132.Stamping the Grid/Solution.py index 9f332811443c0..01410b59c171f 100644 --- a/solution/2100-2199/2132.Stamping the Grid/Solution.py +++ b/solution/2100-2199/2132.Stamping the Grid/Solution.py @@ -1,28 +1,24 @@ -class Solution: - def possibleToStamp( - self, grid: List[List[int]], stampHeight: int, stampWidth: int - ) -> bool: - m, n = len(grid), len(grid[0]) - s = [[0] * (n + 1) for _ in range(m + 1)] - for i, row in enumerate(grid): - for j, v in enumerate(row): - s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + v - - d = [[0] * (n + 1) for _ in range(m + 1)] - for i, row in enumerate(grid): - for j, v in enumerate(row): - if v == 0: - x, y = i + stampHeight, j + stampWidth - if x <= m and y <= n and s[x][y] - s[x][j] - s[i][y] + s[i][j] == 0: - d[i][j] += 1 - d[i][y] -= 1 - d[x][j] -= 1 - d[x][y] += 1 - - cnt = [[0] * (n + 1) for _ in range(m + 1)] - for i, row in enumerate(grid): - for j, v in enumerate(row): - cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j] - if v == 0 and cnt[i + 1][j + 1] == 0: - return False - return True +class Solution: + def possibleToStamp( + self, grid: List[List[int]], stampHeight: int, stampWidth: int + ) -> bool: + m, n = len(grid), len(grid[0]) + s = [[0] * (n + 1) for _ in range(m + 1)] + for i, row in enumerate(grid, 1): + for j, v in enumerate(row, 1): + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + v + d = [[0] * (n + 2) for _ in range(m + 2)] + for i in range(1, m - stampHeight + 2): + for j in range(1, n - stampWidth + 2): + x, y = i + stampHeight - 1, j + stampWidth - 1 + if s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] == 0: + d[i][j] += 1 + d[i][y + 1] -= 1 + d[x + 1][j] -= 1 + d[x + 1][y + 1] += 1 + for i, row in enumerate(grid, 1): + for j, v in enumerate(row, 1): + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1] + if v == 0 and d[i][j] == 0: + return False + return True diff --git a/solution/2100-2199/2132.Stamping the Grid/Solution.ts b/solution/2100-2199/2132.Stamping the Grid/Solution.ts new file mode 100644 index 0000000000000..37b7b2e1a849f --- /dev/null +++ b/solution/2100-2199/2132.Stamping the Grid/Solution.ts @@ -0,0 +1,33 @@ +function possibleToStamp(grid: number[][], stampHeight: number, stampWidth: number): boolean { + const m = grid.length; + const n = grid[0].length; + const s: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + grid[i - 1][j - 1]; + } + } + + const d: number[][] = Array.from({ length: m + 2 }, () => Array(n + 2).fill(0)); + for (let i = 1; i + stampHeight - 1 <= m; ++i) { + for (let j = 1; j + stampWidth - 1 <= n; ++j) { + const [x, y] = [i + stampHeight - 1, j + stampWidth - 1]; + if (s[x][y] - s[x][j - 1] - s[i - 1][y] + s[i - 1][j - 1] === 0) { + d[i][j]++; + d[i][y + 1]--; + d[x + 1][j]--; + d[x + 1][y + 1]++; + } + } + } + + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + d[i][j] += d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1]; + if (grid[i - 1][j - 1] === 0 && d[i][j] === 0) { + return false; + } + } + } + return true; +}