You are given n × m grid and an integer k.
A sensor placed on cell (r, c) covers all cells whose Chebyshev distance from (r, c) is at most k.
The Chebyshev distance between two cells (r1, c1) and (r2, c2) is max(|r1 − r2|,|c1 − c2|).
Your task is to return the minimum number of sensors required to cover every cell of the grid.
Example 1:
Input: n = 5, m = 5, k = 1
Output: 4
Explanation:
Placing sensors at positions (0, 3), (1, 0), (3, 3), and (4, 1) ensures every cell in the grid is covered. Thus, the answer is 4.
Example 2:
Input: n = 2, m = 2, k = 2
Output: 1
Explanation:
With k = 2, a single sensor can cover the entire 2 * 2 grid regardless of its position. Thus, the answer is 1.
Constraints:
1 <= n <= 1031 <= m <= 1030 <= k <= 103Problem Overview: You are given an m x n grid and must place the minimum number of sensors so that every cell is covered. A single sensor covers a fixed square region around its position, which means one sensor protects multiple nearby cells. The task reduces to determining how many such coverage blocks are needed to cover the entire grid.
Approach 1: Grid Simulation / Greedy Placement (O(m * n) time, O(1) space)
A straightforward strategy scans the grid from top-left to bottom-right. Whenever you encounter a cell that has not been covered yet, place a sensor at the top-left position of the next coverage block and mark the cells inside that block as covered. This behaves like tiling the grid with fixed-size squares. Implementation typically uses nested loops and manually advances indices to skip already-covered regions. While the logic works and is easy to reason about, it still iterates through most cells of the grid, leading to O(m * n) time.
This approach demonstrates the core idea of coverage and is useful during interviews to show how you reason about grid traversal and greedy placement decisions.
Approach 2: Mathematical Tiling Insight (O(1) time, O(1) space)
Instead of simulating coverage, observe that each sensor effectively covers a 3 x 3 block of cells. The entire grid can therefore be treated as a tiling problem. Along the rows, you need enough sensors to cover m cells using blocks of size 3, which requires ceil(m / 3). Similarly, columns require ceil(n / 3). Since sensors cover both directions simultaneously, the total number of sensors becomes:
ceil(m / 3) * ceil(n / 3)
This works because any grid can be partitioned into these coverage blocks, and partial leftover rows or columns still require one full sensor placement. The result is a constant-time formula that avoids iterating through the grid entirely.
The key insight comes from recognizing the repeating coverage pattern and converting a grid simulation into a direct formula using math. Problems like this often appear under grid optimization or tiling patterns and can sometimes also be framed as simplified implementation tasks.
Recommended for interviews: Start with the simulation idea to show you understand how sensors cover the grid. Then derive the tiling observation that each sensor covers a fixed block and compute the count using ceiling division. Interviewers typically expect the mathematical solution because it reduces the problem from O(m * n) to O(1) while keeping space usage constant.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Grid Simulation / Greedy Placement | O(m * n) | O(1) | When first reasoning about coverage or demonstrating the placement logic explicitly |
| Mathematical Tiling Formula | O(1) | O(1) | Optimal solution using grid tiling observation and ceiling division |
3648. Minimum Sensors to Cover Grid | Biweekly Contest 163 | Full Explanation + Code • Mask Coder • 777 views views
Watch 8 more video solutions →Practice Minimum Sensors to Cover Grid with our built-in code editor and test cases.
Practice on FleetCode