Class Solution

java.lang.Object
g2101_2200.s2132_stamping_the_grid.Solution

public class Solution extends Object
2132 - Stamping the Grid.<p>Hard</p> <p>You are given an <code>m x n</code> binary matrix <code>grid</code> where each cell is either <code>0</code> (empty) or <code>1</code> (occupied).</p> <p>You are then given stamps of size <code>stampHeight x stampWidth</code>. We want to fit the stamps such that they follow the given <strong>restrictions</strong> and <strong>requirements</strong>:</p> <ol> <li>Cover all the <strong>empty</strong> cells.</li> <li>Do not cover any of the <strong>occupied</strong> cells.</li> <li>We can put as <strong>many</strong> stamps as we want.</li> <li>Stamps can <strong>overlap</strong> with each other.</li> <li>Stamps are not allowed to be <strong>rotated</strong>.</li> <li>Stamps must stay completely <strong>inside</strong> the grid.</li> </ol> <p>Return <code>true</code> <em>if it is possible to fit the stamps while following the given restrictions and requirements. Otherwise, return</em> <code>false</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/11/03/ex1.png" alt="" /></p> <p><strong>Input:</strong> 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</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong> We have two overlapping stamps (labeled 1 and 2 in the image) that are able to cover all the empty cells.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/11/03/ex2.png" alt="" /></p> <p><strong>Input:</strong> grid = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], stampHeight = 2, stampWidth = 2</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong> There is no way to fit the stamps onto all the empty cells without the stamps going outside the grid.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[r].length</code></li> <li><code>1 <= m, n <= 10<sup>5</sup></code></li> <li><code>1 <= m * n <= 2 * 10<sup>5</sup></code></li> <li><code>grid[r][c]</code> is either <code>0</code> or <code>1</code>.</li> <li><code>1 <= stampHeight, stampWidth <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • possibleToStamp

      public boolean possibleToStamp(int[][] grid, int h, int w)