Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum Area Rectangle With Point Constraints I

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3380" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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: Can be formed using four of these points as its corners. Does not contain any other point inside or on its border. Has its edges parallel to the axes. 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 <= 10 points[i].length == 2 0 <= xi, yi <= 100 All the given points are unique.

Explanation

Here's the breakdown of the solution:

  • Identify Potential Rectangles: Iterate through all possible combinations of four points to identify potential rectangles.
  • Check for Validity: For each potential rectangle, verify if its edges are parallel to the axes and if any other point lies inside or on the border of the rectangle.
  • Calculate Area: If a valid rectangle is found, calculate its area and keep track of the maximum area found so far.

  • Runtime Complexity: O(n5), where n is the number of points. Storage Complexity: O(1).

Code

    def max_rectangle_area(points):
    """
    Finds the maximum area of a rectangle formed by four points with edges parallel to the axes,
    without containing any other point inside or on its border.

    Args:
        points: A list of lists, where each inner list represents a point [x, y].

    Returns:
        The maximum area of a valid rectangle, or -1 if no such rectangle is possible.
    """

    n = len(points)
    max_area = -1

    for i in range(n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
                for l in range(k + 1, n):
                    p1, p2, p3, p4 = points[i], points[j], points[k], points[l]

                    # Check if it can form a rectangle with edges parallel to the axes
                    x_values = sorted([p1[0], p2[0], p3[0], p4[0]])
                    y_values = sorted([p1[1], p2[1], p3[1], p4[1]])

                    if x_values[0] == x_values[1] and x_values[2] == x_values[3] and y_values[0] == y_values[1] and y_values[2] == y_values[3] and x_values[0] != x_values[2] and y_values[0] != y_values[2]:
                        # It's a rectangle
                        x1, x2 = x_values[0], x_values[2]
                        y1, y2 = y_values[0], y_values[2]

                        # Check if any other point is inside or on the border
                        valid = True
                        for m in range(n):
                            if m not in (i, j, k, l):
                                px, py = points[m]
                                if x1 <= px <= x2 and y1 <= py <= y2:
                                    valid = False
                                    break

                        if valid:
                            area = abs(x2 - x1) * abs(y2 - y1)
                            max_area = max(max_area, area)

    return max_area

More from this blog

C

Chatmagic blog

2894 posts