Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1637. Widest Vertical Area Between Two Points Containing No Points - Leetcode Solution

Code Implementation

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;
};
      

Problem Description

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.

  • Each point is unique.
  • There is always at least one valid vertical area.
  • You must not reuse points; only consider the x-coordinates of the given points.

Thought Process

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.

Solution Approach

Let's break down the solution step by step:

  1. Extract all x-coordinates: Since vertical areas are defined by x-positions, we only care about the x-values of the points.
  2. Sort the x-coordinates: Sorting arranges the points in increasing order along the x-axis, so we can easily look for gaps.
  3. Find the maximum gap: Iterate through the sorted list and compute the difference between each pair of consecutive x-coordinates. The largest such difference is the answer.
  4. Return the result: The maximal difference found is the width of the widest vertical area containing no points.

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.

Example Walkthrough

Let's consider the input: points = [[8,7],[9,9],[7,4],[9,7]]

  • Extract the x-coordinates: [8, 9, 7, 9]
  • Sort them: [7, 8, 9, 9]
  • Find gaps:
    • Between 7 and 8: 8 - 7 = 1
    • Between 8 and 9: 9 - 8 = 1
    • Between 9 and 9: 9 - 9 = 0
  • The maximum gap is 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.

Time and Space Complexity

  • Brute-force approach: For every pair of points, check if there are any points between them. This would be O(n^2 * n) (choosing pairs and scanning all points), which is not feasible for large input.
  • Optimized approach:
    • Extracting x-coordinates: O(n)
    • Sorting: O(n \log n)
    • Finding the maximum gap: O(n)
    Total: 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.

Summary

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.