Stamping the Grid - Problem
Stamping the Grid is a challenging matrix problem that simulates placing rectangular stamps on a grid. You are given an m Γ— n binary matrix where 0 represents empty cells and 1 represents occupied cells.

Your task is to determine if you can place rectangular stamps of size stampHeight Γ— stampWidth to cover all empty cells while following these rules:

βœ… Must cover all empty cells (0s)
❌ Cannot cover any occupied cells (1s)
πŸ”„ Stamps can overlap with each other
πŸ“ Stamps cannot be rotated
πŸ“¦ Stamps must stay completely inside the grid

Return true if it's possible to stamp all empty cells, false otherwise.

Input & Output

example_1.py β€” Basic Grid
$ 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 = 3
β€Ί Output: true
πŸ’‘ Note: We can place one 4Γ—3 stamp starting at position (0,1) to cover all the empty cells in columns 1-3. The stamp doesn't overlap with any occupied cells (the 1s in column 0).
example_2.py β€” Impossible Case
$ Input: grid = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], stampHeight = 2, stampWidth = 2
β€Ί Output: false
πŸ’‘ Note: No 2Γ—2 stamp can be placed without covering at least one occupied cell (1). The diagonal pattern of obstacles makes it impossible to place any stamp.
example_3.py β€” Multiple Stamps Needed
$ Input: grid = [[1,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,1]], stampHeight = 2, stampWidth = 2
β€Ί Output: true
πŸ’‘ Note: We can place a 2Γ—2 stamp at position (1,1) to cover the 2Γ—2 area of empty cells in the middle. This covers all empty cells without touching the occupied corners.

Visualization

Tap to expand
🏠 Stamping the Grid - Floor Tiling AnalogyStep 1: Room LayoutπŸͺ‘πŸ›οΈπŸͺ‘ = Obstacles (1s)Green = Empty floor (0s)Step 2: Try Tile PlacementπŸͺ‘βŒπŸŸ¨ = Valid tile area❌ = Invalid (hits obstacle)Step 3: Final SolutionπŸͺ‘πŸ›οΈπŸŸ¦ = Tile AπŸŸͺ = Tile BAlgorithm Steps1. Prefix Sum Construction:β€’ Build 2D prefix sum for O(1) range queriesβ€’ Quickly check if any stamp area contains obstacles2. Valid Position Detection:β€’ For each possible stamp position, check if sum of area = 0β€’ Mark positions where stamps can be placed3. Coverage Verification:β€’ For each empty cell, check if it can be covered by β‰₯1 stamp
Understanding the Visualization
1
Identify Obstacles
Mark all occupied cells (furniture) that cannot be covered
2
Find Valid Tile Positions
Determine all positions where a tile can be placed without hitting obstacles
3
Check Coverage
Verify that every empty floor space can be covered by at least one valid tile placement
Key Takeaway
🎯 Key Insight: Use 2D prefix sums to transform expensive O(stampHeightΓ—stampWidth) range checks into O(1) operations, making the overall algorithm run in optimal O(mΓ—n) time.

Time & Space Complexity

Time Complexity
⏱️
O(m*n)

Single pass through grid with O(1) range queries using prefix sums

n
2n
βœ“ Linear Growth
Space Complexity
O(m*n)

Additional space for prefix sum arrays and coverage matrix

n
2n
⚑ Linearithmic Space

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
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 28
67.2K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen