Stamping the Grid - Problem
You are given an m x n binary matrix grid where each cell is either 0 (empty) or 1 (occupied).
You are then given stamps of size stampHeight x stampWidth. We want to fit the stamps such that they follow the given restrictions and requirements:
- Cover all the empty cells.
- Do not cover any of the occupied cells.
- We can put as many stamps as we want.
- Stamps can overlap with each other.
- Stamps are not allowed to be rotated.
- Stamps must stay completely inside the grid.
Return true if it is possible to fit the stamps while following the given restrictions and requirements. Otherwise, return false.
Input & Output
Example 1 — Basic Case
$
Input:
grid = [[1,0,0,0],[1,0,0,0],[1,0,0,0]], stampHeight = 1, stampWidth = 3
›
Output:
true
💡 Note:
We can place 1×3 stamps at positions (0,1), (1,1), and (2,1) to cover all empty cells without covering any occupied cells
Example 2 — Impossible Case
$
Input:
grid = [[1,0,0,0],[0,1,0,0],[0,0,1,0]], stampHeight = 2, stampWidth = 2
›
Output:
false
💡 Note:
No 2×2 stamp can be placed without covering at least one occupied cell (1), so it's impossible to cover all empty cells
Example 3 — Small Stamp
$
Input:
grid = [[1,0,0],[1,0,0],[1,0,0]], stampHeight = 1, stampWidth = 1
›
Output:
true
💡 Note:
1×1 stamps can be placed at all empty positions (0,1), (0,2), (1,1), (1,2), (2,1), (2,2) to cover everything
Constraints
- m == grid.length
- n == grid[r].length
- 1 ≤ m, n ≤ 105
- 1 ≤ m × n ≤ 2 × 105
- grid[r][c] is either 0 or 1
- 1 ≤ stampHeight, stampWidth ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input Grid
Binary matrix with occupied (1) and empty (0) cells
2
Stamp Placement
Place rectangular stamps to cover empty cells only
3
Coverage Check
Verify all empty cells are covered by at least one stamp
Key Takeaway
🎯 Key Insight: Check if every empty cell can be covered by at least one valid stamp placement
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code