You are given an array points where points[i] = [xi, yi] represents the coordinates of a point on an infinite plane.
Your task is to find the maximum area of a rectangle that:
Return the maximum area that you can obtain or -1 if no such rectangle is possible.
Example 1:
Input: points = [[1,1],[1,3],[3,1],[3,3]]
Output: 4
Explanation:

We can make a rectangle with these 4 points as corners and there is no other point that lies inside or on the border. Hence, the maximum possible area would be 4.
Example 2:
Input: points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: -1
Explanation:

There is only one rectangle possible is with points [1,1], [1,3], [3,1] and [3,3] but [2,2] will always lie inside it. Hence, returning -1.
Example 3:
Input: points = [[1,1],[1,3],[3,1],[3,3],[1,2],[3,2]]
Output: 2
Explanation:

The maximum area rectangle is formed by the points [1,3], [1,2], [3,2], [3,3], which has an area of 2. Additionally, the points [1,1], [1,2], [3,1], [3,2] also form a valid rectangle with the same area.
Constraints:
1 <= points.length <= 10points[i].length == 20 <= xi, yi <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the biggest rectangle, we'll try every possible rectangle we can make from the given points. For each potential rectangle, we'll check if it meets the requirements of the problem.
Here's how the algorithm would work step-by-step:
def max_area_rectangle_brute_force(points):
max_area = 0
number_of_points = len(points)
for first_point_index in range(number_of_points):
for second_point_index in range(first_point_index + 1, number_of_points):
point_one = points[first_point_index]
point_two = points[second_point_index]
# Define the rectangle using the two points as corners.
x_coordinates = sorted([point_one[0], point_two[0]])
y_coordinates = sorted([point_one[1], point_two[1]])
bottom_left = (x_coordinates[0], y_coordinates[0])
top_right = (x_coordinates[1], y_coordinates[1])
is_valid = True
# Check if any point is inside the rectangle.
for other_point in points:
if (bottom_left[0] < other_point[0] < top_right[0] and\
bottom_left[1] < other_point[1] < top_right[1]):
is_valid = False
break
if is_valid:
# Only calculate area for valid rectangles.
area = (top_right[0] - bottom_left[0]) * (top_right[1] - bottom_left[1])
max_area = max(max_area, area)
return max_areaThe optimal strategy avoids checking every possible rectangle by focusing on finding the largest possible width and height that satisfy the point constraints. We efficiently determine these dimensions by sorting and clever comparisons. This eliminates a large number of unnecessary calculations.
Here's how the algorithm would work step-by-step:
def maximum_area_rectangle(points):
maximum_area = 0
points.sort(key=lambda point: point[0])
for right_edge_index in range(len(points)):
right_x = points[right_edge_index][0]
for left_edge_index in range(right_edge_index):
left_x = points[left_edge_index][0]
width = right_x - left_x
#Consider only points with x coordinates between the left and right edge
valid_points = [point for point in points if left_x <= point[0] <= right_x]
valid_points.sort(key=lambda point: point[1])
#Iterate to check top and bottom edges to find the max height
for top_edge_index in range(len(valid_points)):
bottom_y = valid_points[top_edge_index][1]
for bottom_edge_index in range(top_edge_index + 1, len(valid_points)):
top_y = valid_points[bottom_edge_index][1]
height = top_y - bottom_y
area = width * height
#Store the max area calculated
maximum_area = max(maximum_area, area)
return maximum_area| Case | How to Handle |
|---|---|
| Points array is null or empty | Return 0 as no rectangle can be formed. |
| Points array contains fewer than 2 points | Return 0 since a rectangle needs at least two points. |
| All points are on the same line (horizontal or vertical) | Return 0 because a rectangle requires at least two distinct horizontal and vertical coordinates. |
| Points have duplicate x or y coordinates, but no true rectangle exists | The algorithm should correctly identify that no valid rectangle can be formed and return 0. |
| Integer overflow when calculating area | Use a larger data type (e.g., long) to store the area or check for overflow before calculating. |
| Points array contains points with identical coordinates | The algorithm must handle duplicate points gracefully, possibly by skipping them or treating them as a single point. |
| Maximum number of points leading to performance issues (O(n^2) or worse) | Optimize the algorithm to improve its time complexity, possibly by using appropriate data structures. |
| Points array contains negative coordinates | The solution should correctly handle negative coordinates when calculating the area of the rectangle. |