↓Skip to main content
  1. LeetCode/

LeetCode 3648: Minimum Sensors to Cover Grid

·2 mins· ·
LeetCode Medium Math
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-08-16

  • Submissions: Wrong Answer → Accepted

Approach
#

Chebyshev distance <= k covers a square of side 2k + 1. The two directions are independent:

  • Rows need ceil(n / (2k + 1)) sensors.
  • Columns need ceil(m / (2k + 1)).

The answer is the product.

My Attempts
#

Attempt 1 wrote n // (2k + 1) + 1, one too many when it divides evenly. For n = 9, m = 3, k = 0 each sensor covers only itself, so the answer is 27; it computed 10 × 4 = 40. Adding 1 only when there is a remainder passed.

Solution
#

class Solution:
    def minSensors(self, n: int, m: int, k: int) -> int:
        side = 2 * k + 1
        return -(-n // side) * -(-m // side)       # ceiling division

Complexity
#

Time O(1), space O(1).

Mistakes to Avoid
#

  • Ceiling division as a // b + 1: one too many on exact division. Use -(-a // b) or (a + b - 1) // b.
  • Testing divisibility with floating point a / b == a // b: large values can be inexact.

Interview Explanation
#

Each sensor covers a square of side 2k + 1, and rows and columns are independent, needing n and m divided by the side with rounding up; their product is the answer. Time O(1).

Related

LeetCode 3627: Maximum Median Sum of Subsequences of Size 3
·2 mins
LeetCode Medium Math Greedy Sorting Game-Theory Array
LeetCode 3627 note: after sorting, each group takes the two largest and the smallest, with the second largest as the median; the answer takes every other value from the end, n / 3 of them
LeetCode 1814: Count Nice Pairs in an Array
·1 min
LeetCode Medium Array Hash-Table Math Counting
LeetCode 1814 note: rearrange the condition into nums[i] - rev(nums[i]) == nums[j] - rev(nums[j]), count pairs per value, and remember the modulus
LeetCode 313: Super Ugly Number
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 313 note: generalizing LC 264 with one pointer per prime; with a heap, avoid pushing the same number many times
LeetCode 396: Rotate Function
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 396 note: F(k) and F(k - 1) differ by one formula: F(k) = F(k - 1) + sum - n × the element moved to the front
LeetCode 264: Ugly Number II
·2 mins
LeetCode Medium Hash-Table Math Dynamic-Programming Heap-Priority-Queue
LeetCode 264 note: three pointers mark the next ugly numbers to multiply by 2, 3 and 5; take the minimum each time and advance every pointer that produced it
LeetCode 50: Pow(x, n)
·2 mins
LeetCode Medium Math Recursion
LeetCode 50 note: fast exponentiation, x^n = (x²)^(n/2) with an extra x for odd n; for negative n compute 1 / x^(-n)