Maximum Area Rectangle With Point Constraints II Problem
Description
LeetCode Problem 3382.
There are n points on an infinite plane. You are given two integer arrays xCoord and yCoord where (xCoord[i], yCoord[i]) represents the coordinates of the i^th point.
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: xCoord = [1,1,3,3], yCoord = [1,3,1,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: xCoord = [1,1,3,3,2], yCoord = [1,3,1,3,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: xCoord = [1,1,3,3,1,3], yCoord = [1,3,1,3,2,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 <= xCoord.length == yCoord.length <= 2 * 10^50 <= xCoord[i], yCoord[i] <= 8 * 10^7- All the given points are unique.
Solution
Approach: Sweep by column + Fenwick tree
A valid rectangle has a vertical edge on its left and on its right. Each vertical edge joins two points that are neighbours in the same column (sorted by y), because any other point between them would lie on the border.
Group the points by x and go through the columns from left to right. For each pair of neighbouring points (y1, y2) in a column, remember the last column where the same pair was neighbours. If there is one, the two columns form a candidate rectangle (an earlier column with the same pair would contain the nearer column's corners inside it, so it is never valid).
The candidate is valid exactly when the box x in [left, right], y in [y1, y2] contains just the 4 corners. Count points in a box offline with a Fenwick tree over compressed y values: take the count of the y range just before inserting the left column and again right after inserting the right column; the difference must be 4. The answer is the largest valid area, or -1.
Complexity: Time O(n log n), space O(n).
class Solution:
def maxRectangleArea(self, xCoord: List[int], yCoord: List[int]) -> int:
ys = sorted(set(yCoord))
rank = {y: i + 1 for i, y in enumerate(ys)}
m = len(ys)
cols = {}
for x, y in zip(xCoord, yCoord):
cols.setdefault(x, []).append(y)
xs = sorted(cols)
# a vertical edge joins two neighbouring points of a column;
# pair it with the previous column that has the same edge
last = {}
starts = [[] for _ in xs]
ends = [[] for _ in xs]
q = []
for c, x in enumerate(xs):
col = sorted(cols[x])
for a, b in zip(col, col[1:]):
key = a * (1 << 27) + b
if key in last:
q.append((rank[a], rank[b], (x - xs[last[key]]) * (b - a)))
starts[last[key]].append(len(q) - 1)
ends[c].append(len(q) - 1)
last[key] = c
tree = [0] * (m + 1)
def add(i):
while i <= m:
tree[i] += 1
i += i & -i
def query(i):
s = 0
while i > 0:
s += tree[i]
i -= i & -i
return s
before = [0] * len(q)
ans = -1
for c, x in enumerate(xs):
for j in starts[c]:
before[j] = query(q[j][1]) - query(q[j][0] - 1)
for y in cols[x]:
add(rank[y])
for j in ends[c]:
inside = query(q[j][1]) - query(q[j][0] - 1)
if inside - before[j] == 4:
ans = max(ans, q[j][2])
return ans