Minimum Area Rectangle

update Dec 30 18:03, 2018

LeetCodearrow-up-right

Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.

If there isn't any rectangle, return 0.

Example 1:

Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4

Example 2:

Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2

Note:

  1. 1 <= points.length <= 500

  2. 0 <= points[i][0] <= 40000

  3. 0 <= points[i][1] <= 40000

  4. All points are distinct.

Basic Idea:

因为只考虑平行于坐标轴的矩形,这种情况下一条对角线就可以确定一个矩形。我们只需要枚举所有对角线,检查另外两点是否存在,然后就可以计算矩形面积。时间复杂度 O(n^2),因为共有 n(n-1)/2 个连线。

  • Java Code: