Ctrl K

Smallest Paddock

Medium
Posts are driven into a field; their coordinates are given in posts, where posts[i] = [x, y]. No two posts share a spot. A farmer wants to fence a rectangular paddock whose sides run parallel to the axes and whose four corners are all posts from the list. Other posts may end up inside the paddock or on its sides; that is fine. Return the smallest possible area of such a paddock. If no rectangle can be formed, return 0.
Example 1
posts=[[0, 0], [0, 2], [5, 0], [5, 2], [3, 0], [3, 2], [1, 1]] → 4

Rectangles 3 × 2, 2 × 2 and 5 × 2 are possible. The smallest lies between x = 3 and x = 5, with area 4.

Example 2
posts=[[1, 1], [2, 2], [3, 1], [2, 4]] → 0

No four posts form a rectangle with sides along the axes, so the answer is 0.

Example 3
posts=[[2, 7], [6, 7], [2, 10], [6, 10]] → 12

The four posts are the corners of a single 4 × 3 rectangle, area 12.

Constraints
  • 1 ≤ posts.length ≤ 500
  • posts[i].length = 2
  • 0 ≤ x, y ≤ 4 · 10⁴
  • All points are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNearest Points to the Base MediumFewest Lines Through the Dots MediumGrid Points Under Circles MediumSegments on a Chart MediumPicks Without a Fixed Gap Medium
loading Python…
Input
posts=[[0, 0], [0, 2], [5, 0], [5, 2], [3, 0], [3, 2], [1, 1]]
Expected
4

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