Ctrl K

Empty Boxes Between Points

Medium
A site plan has points spots marked on it, each given as [x, y]. The y axis points up. An ordered pair of different points (A, B) counts if: - A is neither to the right of nor below B, that is A.x ≤ B.x and A.y ≥ B.y: A is the upper-left corner and B the lower-right one; - the rectangle with these corners, border included, contains no other point from spots. When the two points share an x or a y, the rectangle collapses into a segment and the same rule applies: no other point may lie on that segment. Return the number of pairs that count.
Example 1
spots=[[0, 4], [2, 2], [4, 0]] → 2

The pairs ([0, 4], [2, 2]) and ([2, 2], [4, 0]) work. The rectangle of ([0, 4], [4, 0]) contains [2, 2], so that pair does not count.

Example 2
spots=[[1, 1], [3, 5], [5, 3]] → 1

Only ([3, 5], [5, 3]) works. The point [1, 1] is to the left of and below both others, so in no pair can it be either the upper-left or the lower-right corner.

Example 3
spots=[[2, 6], [2, 3], [2, 1]] → 2

All points share one vertical line. ([2, 6], [2, 3]) and ([2, 3], [2, 1]) work, the rectangle collapses into a segment. [2, 3] lies between [2, 6] and [2, 1].

Constraints
  • 2 ≤ len(spots) ≤ 50
  • spots[i] = [x, y], 0 ≤ x, y ≤ 50
  • All points are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEmpty Rectangle From Marked Points MediumCorner Pairs With No One Between HardSmallest Paddock MediumNearest Points to the Base MediumGrid Points Under Circles Medium
loading Python…
Input
spots=[[0, 4], [2, 2], [4, 0]]
Expected
2

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