Input: grid =[[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0]], stampHeight =4, stampWidth =3Output: trueExplanation: We have two overlapping stamps(labeled 1 and 2in the image) that are able to cover all the empty cells.
Input: grid =[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], stampHeight =2, stampWidth =2Output: falseExplanation: There is no way to fit the stamps onto all the empty cells without the stamps going outside the grid.
To efficiently check if all empty cells can be covered by stamps without overlapping occupied cells, we use a 2D prefix sum to quickly verify if a stamp can be placed at any position. We then use another prefix sum to mark all cells covered by at least one stamp and check if every empty cell is covered.
#include<vector>usingnamespace std;
classSolution {
public:bool possibleToStamp(vector<vector<int>>& grid, int stampHeight, int stampWidth) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> ps(m+1, vector<int>(n+1));
for (int i =0; i < m; ++i)
for (int j =0; j < n; ++j)
ps[i+1][j+1] = grid[i][j] + ps[i][j+1] + ps[i+1][j] - ps[i][j];
vector<vector<int>> diff(m+2, vector<int>(n+2));
for (int i = stampHeight; i <= m; ++i) {
for (int j = stampWidth; j <= n; ++j) {
int x1 = i - stampHeight, y1 = j - stampWidth;
int sum = ps[i][j] - ps[x1][j] - ps[i][y1] + ps[x1][y1];
if (sum ==0) {
diff[x1][y1]++;
diff[x1][j]--;
diff[i][y1]--;
diff[i][j]++;
}
}
}
for (int i =0; i < m; ++i) {
for (int j =0; j < n; ++j) {
if (grid[i][j] ==0) {
int cov =0;
for (int x =0; x <= i; ++x)
for (int y =0; y <= j; ++y)
cov += diff[x][y];
if (cov ==0) return false;
}
}
}
return true;
}
};
classSolution {
publicbooleanpossibleToStamp(int[][] grid, int stampHeight, int stampWidth) {
int m = grid.length, n = grid[0].length;
int[][] ps =newint[m+1][n+1];
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
ps[i+1][j+1]= grid[i][j]+ ps[i][j+1]+ ps[i+1][j]- ps[i][j];
int[][] diff =newint[m+2][n+2];
for (int i = stampHeight; i <= m; ++i) {
for (int j = stampWidth; j <= n; ++j) {
int x1 = i - stampHeight, y1 = j - stampWidth;
int sum = ps[i][j]- ps[x1][j]- ps[i][y1]+ ps[x1][y1];
if (sum == 0) {
diff[x1][y1]++;
diff[x1][j]--;
diff[i][y1]--;
diff[i][j]++;
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j]== 0) {
int cov = 0;
for (int x = 0; x <= i; ++x)
for (int y = 0; y <= j; ++y)
cov += diff[x][y];
if (cov == 0) returnfalse;
}
}
}
returntrue;
}
}
classSolution {
funpossibleToStamp(grid: Array<IntArray>, stampHeight: Int, stampWidth: Int): Boolean {
val m = grid.size
val n = grid[0].size
val ps = Array(m+1) { IntArray(n+1) }
for (i in0 until m)
for (j in0 until n)
ps[i+1][j+1] = grid[i][j] + ps[i][j+1] + ps[i+1][j] - ps[i][j]
val diff = Array(m+2) { IntArray(n+2) }
for (i in stampHeight..m) {
for (j in stampWidth..n) {
val x1 = i - stampHeight
val y1 = j - stampWidth
val sum = ps[i][j] - ps[x1][j] - ps[i][y1] + ps[x1][y1]
if (sum ==0) {
diff[x1][y1]++ diff[x1][j]-- diff[i][y1]-- diff[i][j]++ }
}
}
for (i in0 until m) {
for (j in0 until n) {
if (grid[i][j] ==0) {
var cov = 0for (x in0..i)
for (y in0..j)
cov += diff[x][y]
if (cov ==0) returnfalse }
}
}
returntrue }
}
classSolution:
defpossibleToStamp(self, grid: list[list[int]], stampHeight: int, stampWidth: int) -> bool:
m, n = len(grid), len(grid[0])
ps = [[0]*(n+1) for _ in range(m+1)]
for i in range(m):
for j in range(n):
ps[i+1][j+1] = grid[i][j] + ps[i][j+1] + ps[i+1][j] - ps[i][j]
diff = [[0]*(n+2) for _ in range(m+2)]
for i in range(stampHeight, m+1):
for j in range(stampWidth, n+1):
x1, y1 = i-stampHeight, j-stampWidth
s = ps[i][j] - ps[x1][j] - ps[i][y1] + ps[x1][y1]
if s ==0:
diff[x1][y1] +=1 diff[x1][j] -=1 diff[i][y1] -=1 diff[i][j] +=1for i in range(m):
for j in range(n):
if grid[i][j] ==0:
cov =0for x in range(i+1):
for y in range(j+1):
cov += diff[x][y]
if cov ==0:
returnFalsereturnTrue
impl Solution {
pubfnpossible_to_stamp(grid: Vec<Vec<i32>>, stamp_height: i32, stamp_width: i32) -> bool {
let m = grid.len();
let n = grid[0].len();
letmut ps =vec![vec![0; n+1]; m+1];
for i in0..m {
for j in0..n {
ps[i+1][j+1] = grid[i][j] + ps[i][j+1] + ps[i+1][j] - ps[i][j];
}
}
letmut diff =vec![vec![0; n+2]; m+2];
for i in stamp_height asusize..=m {
for j in stamp_width asusize..=n {
let x1 = i - stamp_height asusize;
let y1 = j - stamp_width asusize;
let sum = ps[i][j] - ps[x1][j] - ps[i][y1] + ps[x1][y1];
if sum ==0 {
diff[x1][y1] +=1;
diff[x1][j] -=1;
diff[i][y1] -=1;
diff[i][j] +=1;
}
}
}
for i in0..m {
for j in0..n {
if grid[i][j] ==0 {
letmut cov =0;
for x in0..=i {
for y in0..=j {
cov += diff[x][y];
}
}
if cov ==0 {
returnfalse;
}
}
}
}
true }
}