LeetCode Problem Workspace

Maximum Area Rectangle With Point Constraints II

Find the largest rectangle on a plane using given points while avoiding any interior points and optimizing with math and arrays.

category

6

Topics

code_blocks

0

Code langs

hub

3

Related

Practice Focus

Hard · Array plus Math

bolt

Answer-first summary

Find the largest rectangle on a plane using given points while avoiding any interior points and optimizing with math and arrays.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array plus Math

Try AiBox Copilotarrow_forward

This problem requires calculating the maximum area rectangle formed by specific points while ensuring no other point lies inside or on its edges. Start by sorting points by x-coordinate and leverage array and math operations to track potential rectangles efficiently. Use data structures like segment trees or binary indexed trees to speed up range checks for interior points, ensuring an optimized solution under tight constraints.

Problem Statement

You are given n unique points on an infinite 2D plane represented by two arrays xCoord and yCoord. Each point (xCoord[i], yCoord[i]) denotes the coordinates of the ith point. Your task is to analyze these points and determine feasible rectangles based strictly on the points themselves.

A valid rectangle is one whose four corners are among the given points and contains no other point from the set inside or on its border. Compute the maximum area achievable by such a rectangle or return -1 if no rectangle meets the criteria.

Examples

Example 1

Input: xCoord = [1,1,3,3], yCoord = [1,3,1,3]

Output: 4

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: xCoord = [1,1,3,3,2], yCoord = [1,3,1,3,2]

Output: -1

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: xCoord = [1,1,3,3,1,3], yCoord = [1,3,1,3,2,2]

Output: 2

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 <= xCoord.length == yCoord.length <= 2 * 105
  • 0 <= xCoord[i], yCoord[i] <= 8 * 107
  • All the given points are unique.

Solution Approach

Sort points and iterate

Begin by sorting points based on x-coordinate to simplify rectangle candidate selection. Iterate over point pairs to identify potential horizontal edges while tracking vertical ranges. This array-based ordering reduces unnecessary comparisons and aligns with the problem's array plus math pattern.

Use data structures to check interiors

Maintain a segment tree or binary indexed tree to quickly determine if any point exists inside a candidate rectangle. This approach ensures no rectangle is invalidated due to hidden interior points, which is the common failure mode of naive solutions.

Calculate area and update maximum

For each valid rectangle determined from sorted points and range checks, compute the area as width times height. Track the maximum value encountered and return it at the end. This direct math calculation tied with array scanning guarantees correctness while handling large datasets efficiently.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity varies depending on the range-checking data structure: O(n^2 log n) with segment trees or binary indexed trees for interior checks. Space complexity depends on auxiliary structures used to store ranges and mappings, typically O(n).

What Interviewers Usually Probe

  • Sorting points by x-coordinate indicates awareness of array plus math pattern.
  • Using a segment tree or BIT shows you understand efficient interior point queries.
  • Tracking rectangle areas while avoiding interior points reveals correct handling of geometric constraints.

Common Pitfalls or Variants

Common pitfalls

  • Ignoring interior points leads to incorrect maximum area calculations.
  • Not sorting points can cause unnecessary comparisons and timeouts on large inputs.
  • Failing to properly update maximum area when multiple rectangles yield same area can misreport the result.

Follow-up variants

  • Consider points in 3D space to maximize cuboid volume with similar constraints.
  • Restrict rectangles to axis-aligned with specific minimum or maximum side lengths.
  • Find the number of valid rectangles rather than just the maximum area.

FAQ

What is the key pattern in Maximum Area Rectangle With Point Constraints II?

The primary pattern is array plus math: sort points and use array scanning combined with geometric calculations to compute maximum rectangle area.

Can a rectangle include points on its border?

No, any rectangle including a point inside or on its border other than the corners is invalid.

Which data structures help in checking interior points efficiently?

Segment trees or binary indexed trees allow fast range queries to verify that no interior points exist within candidate rectangles.

What is the output if no rectangle can be formed?

Return -1 if all candidate rectangles are invalidated by interior points.

How do sorting points by x-coordinate help?

Sorting reduces the number of comparisons when iterating for potential horizontal edges and aligns with the array plus math optimization pattern.

terminal

Solution

Solution 1

#### Python3

1
Maximum Area Rectangle With Point Constraints II Solution: Array plus Math | LeetCode #3382 Hard