Skip to main content

Minimum Sensors to Cover Grid - Solution & Explanation

MediumMath3 min readAsked at: Amazon, Microsoft, Cisco +1
Practice this problem

Problem Statement

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 <= 103
  • 1 <= m <= 103
  • 0 <= k <= 103

Approach Overview

Problem 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

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Grid Simulation / Greedy PlacementO(m * n)O(1)When first reasoning about coverage or demonstrating the placement logic explicitly
Mathematical Tiling FormulaO(1)O(1)Optimal solution using grid tiling observation and ceiling division

Video Solution

3648. Minimum Sensors to Cover Grid | Biweekly Contest 163 | Full Explanation + Code • Let's Aim FAANG • 789 views views

Watch 7 more video solutions →

Frequently Asked Questions

Is Minimum Sensors to Cover Grid easy or hard?
The problem is typically classified as Medium because the key step is recognizing the tiling pattern. Once you identify that each sensor covers a fixed 3 x 3 region, the implementation becomes a simple constant-time formula.
Minimum Sensors to Cover Grid Python/Java solution
In Python or Java, compute the result using ceiling division: (m + 2) // 3 and (n + 2) // 3, then multiply the results. This avoids floating-point operations and directly produces the minimum number of sensors.
How to solve Minimum Sensors to Cover Grid in O(1)?
Recognize that a single sensor covers a 3 x 3 block. Instead of iterating through the grid, compute how many such blocks are required to cover the rows and columns. The answer is ceil(m / 3) * ceil(n / 3), which can be calculated directly in constant time.
What is the best approach for Minimum Sensors to Cover Grid?
The optimal approach uses a mathematical tiling observation. Each sensor covers a 3 x 3 region of the grid, so the grid can be partitioned into coverage blocks. The minimum sensors required is ceil(m / 3) * ceil(n / 3). This runs in O(1) time and constant space.
Is Minimum Sensors to Cover Grid asked at Google/Amazon/Meta?
Grid coverage and tiling problems appear frequently in interviews at companies like Google, Amazon, and Meta. While the exact problem may vary, the pattern of converting grid coverage into a mathematical formula is commonly tested.
What data structure is used in Minimum Sensors to Cover Grid?
The optimal solution does not require any special data structures. It relies purely on mathematical reasoning and integer arithmetic. A simulation-based approach may use simple grid traversal with loops.
What is the time complexity of Minimum Sensors to Cover Grid?
The optimal solution runs in O(1) time because it only performs a few arithmetic operations using ceiling division. A naive simulation that scans the entire grid would take O(m * n) time.

Ready to solve this problem?

Practice Minimum Sensors to Cover Grid with our built-in code editor and test cases.

Practice on FleetCode