Ctrl K

Corner Pairs With No One Between

Hard
Tent spots are marked on a field; their coordinates are given in spots, where spots[i] = [x, y]. The x axis points right and the y axis points up. All spots are distinct, and each will hold exactly one tent. Two friends want to pick spots A and B and fence off an axis-aligned rectangle with A as its upper-left corner and B as its lower-right corner, so A.x ≤ B.x and A.y ≥ B.y. The rectangle may collapse into a segment. A pair works if no other tent stands inside the rectangle or on its border. Return the number of ordered pairs (A, B) that work.
Example 1
spots=[[0, 4], [2, 2], [4, 0]] → 2

The pairs (0, 4) → (2, 2) and (2, 2) → (4, 0) work. The pair (0, 4) → (4, 0) does not: (2, 2) is inside its rectangle.

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

(1, 3) → (1, 1) is a vertical segment and (1, 3) → (3, 3) a horizontal one, both empty. No other pair has its first point up and to the left of the second.

Example 3
spots=[[0, 0], [5, 5]] → 0

(0, 0) is to the left of (5, 5) but below it, so neither order makes a valid upper-left corner.

Constraints
  • 2 ≤ len(spots) ≤ 1000
  • spots[i].length == 2
  • -10⁹ ≤ x, y ≤ 10⁹
  • All points are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEmpty Boxes Between Points MediumEmpty Rectangle From Marked Points MediumBest View for a Turning Camera HardWhere Moss Species Meet HardNearest Pair of Sensors Hard
loading Python…
Input
spots=[[0, 4], [2, 2], [4, 0]]
Expected
2

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