class Solution:
def maxWidthOfVerticalArea(self, points):
xs = sorted(point[0] for point in points)
max_gap = 0
for i in range(1, len(xs)):
max_gap = max(max_gap, xs[i] - xs[i-1])
return max_gap
class Solution {
public:
int maxWidthOfVerticalArea(vector<vector<int>>& points) {
vector<int> xs;
for (const auto& p : points) xs.push_back(p[0]);
sort(xs.begin(), xs.end());
int maxGap = 0;
for (int i = 1; i < xs.size(); ++i)
maxGap = max(maxGap, xs[i] - xs[i-1]);
return maxGap;
}
};
import java.util.*;
class Solution {
public int maxWidthOfVerticalArea(int[][] points) {
int n = points.length;
int[] xs = new int[n];
for (int i = 0; i < n; i++) {
xs[i] = points[i][0];
}
Arrays.sort(xs);
int maxGap = 0;
for (int i = 1; i < n; i++) {
maxGap = Math.max(maxGap, xs[i] - xs[i-1]);
}
return maxGap;
}
}
var maxWidthOfVerticalArea = function(points) {
let xs = points.map(p => p[0]);
xs.sort((a, b) => a - b);
let maxGap = 0;
for (let i = 1; i < xs.length; i++) {
maxGap = Math.max(maxGap, xs[i] - xs[i-1]);
}
return maxGap;
};
You are given a list of points
in the 2D plane, where each point
is represented as [x, y]
. A vertical area is defined as the space between two vertical lines x1
and x2
(where x1 < x2
) such that there are no points with x
-coordinates strictly between x1
and x2
. Your task is to find the maximum width of such an area, i.e., the largest difference x2 - x1
between the x
-coordinates of two points, such that there are no other points with x
-coordinates between them.
x
-coordinates of the given points.
At first glance, we might think to examine all possible pairs of points and check if there are any other points between their x
-coordinates. However, this would require checking every possible pair and then scanning all points for each pair, which is inefficient.
But if we sort all the x
-coordinates, the widest vertical area with no points in between must be the largest gap between two consecutive x
-values. This is because any larger gap between non-consecutive points would necessarily have at least one other point between them, violating the conditions.
This insight allows us to shift from a brute-force approach to a much more efficient one by simply sorting and finding the maximal adjacent difference.
Let's break down the solution step by step:
x
-coordinates: Since vertical areas are defined by x
-positions, we only care about the x
-values of the points.x
-coordinates: Sorting arranges the points in increasing order along the x-axis, so we can easily look for gaps.x
-coordinates. The largest such difference is the answer.We use sorting because it allows us to efficiently find all possible "gaps" between points, and since checking consecutive pairs is sufficient, we avoid unnecessary comparisons.
Let's consider the input: points = [[8,7],[9,9],[7,4],[9,7]]
x
-coordinates: [8, 9, 7, 9]
[7, 8, 9, 9]
8 - 7 = 1
9 - 8 = 1
9 - 9 = 0
1
. So, the widest vertical area is 1
unit wide.At each step, we only consider consecutive differences, and since there are no other points between these pairs, they are valid vertical areas.
O(n^2 * n)
(choosing pairs and scanning all points), which is not feasible for large input.
x
-coordinates: O(n)
O(n \log n)
O(n)
O(n \log n)
time and O(n)
space (for storing the x
-coordinates).
The optimized approach is efficient and suitable for large numbers of points.
The key insight to solving the "Widest Vertical Area Between Two Points Containing No Points" problem is recognizing that the largest empty vertical area corresponds to the biggest gap between consecutive x
-coordinates after sorting. Instead of checking all pairs, sorting enables us to efficiently find the answer with just one pass through the data. This approach is both simple and elegant, leveraging sorting and basic iteration to achieve optimal performance.