Minimum Area of an Axis-Aligned Rectangle
You are given distinct points on the xy-plane. Each point is represented by a string in the format "x,y".
Find the minimum area of a rectangle whose four corners are included in the given points and whose sides are parallel to the x-axis and y-axis. Return 0 if no such rectangle can be formed.
Method Signature
int minimumRectangleArea(List<String> points)
Parameters
points contains the given points.
- Each entry has the format
"x,y", where x and y are the coordinates of one point.
Return Value
Return the smallest area of an axis-aligned rectangle that can be formed using four of the given points as its corners. Return 0 when no rectangle exists.
Constraints
1 <= points.size() <= 500
- Every entry in
points has the format "x,y".
0 <= x <= 40,000
0 <= y <= 40,000
- All given points are distinct.
Examples
Example 1
minimumRectangleArea(points = List.of("0,1", "0,4", "2,1", "2,4", "5,2"))
Output: 6
The points (0,1), (0,4), (2,1), and (2,4) form a rectangle with area 2 * 3 = 6.
Example 2
minimumRectangleArea(points = List.of("1,2", "1,6", "3,2", "3,6", "4,2", "4,6"))
Output: 4
Multiple rectangles can be formed. The smallest uses x-coordinates 3 and 4, giving an area of 1 * 4 = 4.
Example 3
minimumRectangleArea(points = List.of("0,0", "1,2", "2,4", "3,1"))
Output: 0
No four points form a rectangle with sides parallel to the coordinate axes.