Ctrl K

Cover Points With Rectangles

Medium
You are given points points on a plane, where points[i] = [x, y], and a number width. Cover every point with rectangles. Each rectangle stands on the x-axis: its lower-left corner is (x1, 0) and its upper-right corner is (x2, y2), where x1 ≤ x2, y2 ≥ 0 and x2 − x1 ≤ width. The height can be anything. A point is covered if it lies inside a rectangle or on its border; rectangles may overlap. Return the smallest number of rectangles that cover all the points.
Example 1
points=[[4, 7], [0, 2], [6, 1], [1, 9], [9, 3]], width=2 → 3

Three rectangles suffice: x from 0 to 2 (points at x = 0 and 1), from 4 to 6 (x = 4 and 6) and from 9 to 11 (x = 9).

Example 2
points=[[5, 1], [5, 8], [5, 0]], width=0 → 1

All points lie on the line x = 5, so one rectangle of zero width is enough.

Example 3
points=[[0, 0], [3, 3], [6, 6], [7, 1]], width=3 → 2
Constraints
  • 1 ≤ len(points) ≤ 10⁵
  • points[i] = [x, y], 0 ≤ x, y ≤ 10⁹
  • 0 ≤ width ≤ 10⁹
  • All points are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumLow, High, Low MediumStrictly Low, High, Low MediumFewest Meetings to Cancel Medium
loading Python…
Input
points=[[4, 7], [0, 2], [6, 1], [1, 9], [9, 3]], width=2
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 120 tests on the server, hidden ones included.