Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1637.html

1637. Widest Vertical Area Between Two Points Containing No Points (Medium)

Given n points on a 2D plane where points[i] = [xi, yi], Return the widest vertical area between two points such that no points are inside the area.

A vertical area is an area of fixed-width extending infinitely along the y-axis (i.e., infinite height). The widest vertical area is the one with the maximum width.

Note that points on the edge of a vertical area are not considered included in the area.

 

Example 1:

Input: points = [[8,7],[9,9],[7,4],[9,7]]
Output: 1
Explanation: Both the red and the blue area are optimal.

Example 2:

Input: points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]]
Output: 3

 

Constraints:

  • n == points.length
  • 2 <= n <= 105
  • points[i].length == 2
  • 0 <= xi, yi <= 109

Related Topics:
Sort

Solution 1. Sort

Sort the points in ascending order of x values, then traverse to find the maximum distance of x values between adjacent points.

// OJ: https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/
// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
    int maxWidthOfVerticalArea(vector<vector<int>>& A) {
        sort(begin(A), end(A), [](auto &a, auto &b) { return a[0]< b[0]; });
        int ans = 0;
        for (int i = 1; i < A.size(); ++i) ans = max(ans, A[i][0] - A[i - 1][0]);
        return ans;
    }
};
  • class Solution {
        public int maxWidthOfVerticalArea(int[][] points) {
            Arrays.sort(points, new Comparator<int[]>() {
                public int compare(int[] point1, int[] point2) {
                    if (point1[0] != point2[0])
                        return point1[0] - point2[0];
                    else
                        return point1[1] - point2[1];
                }
            });
            int maxWidth = 0;
            int length = points.length;
            for (int i = 1; i < length; i++) {
                int width = points[i][0] - points[i - 1][0];
                maxWidth = Math.max(maxWidth, width);
            }
            return maxWidth;
        }
    }
    
    ############
    
    class Solution {
        public int maxWidthOfVerticalArea(int[][] points) {
            Arrays.sort(points, (a, b) -> a[0] - b[0]);
            int ans = 0;
            for (int i = 0; i < points.length - 1; ++i) {
                ans = Math.max(ans, points[i + 1][0] - points[i][0]);
            }
            return ans;
        }
    }
    
  • // OJ: https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/
    // Time: O(NlogN)
    // Space: O(1)
    class Solution {
    public:
        int maxWidthOfVerticalArea(vector<vector<int>>& A) {
            sort(begin(A), end(A), [](auto &a, auto &b) { return a[0]< b[0]; });
            int ans = 0;
            for (int i = 1; i < A.size(); ++i) ans = max(ans, A[i][0] - A[i - 1][0]);
            return ans;
        }
    };
    
  • class Solution:
        def maxWidthOfVerticalArea(self, points: List[List[int]]) -> int:
            points.sort()
            return max(b[0] - a[0] for a, b in pairwise(points))
    
    ############
    
    # 1637. Widest Vertical Area Between Two Points Containing No Points
    # https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/
    
    class Solution:
        def maxWidthOfVerticalArea(self, points: List[List[int]]) -> int:
            if not points: return 0
            
            points.sort(key = lambda x : (x[0], x[1]))
            
            res = 0
            prevX = points[0][0]
            for x,y in points[1:]:
                if x == prevX:
                    continue
                else:
                    res = max(res, x - prevX)
                prevX = x
            
            return res
    
  • func maxWidthOfVerticalArea(points [][]int) (ans int) {
    	sort.Slice(points, func(i, j int) bool { return points[i][0] < points[j][0] })
    	for i, p := range points[1:] {
    		ans = max(ans, p[0]-points[i][0])
    	}
    	return
    }
    
    func max(a, b int) int {
    	if a > b {
    		return a
    	}
    	return b
    }
    
  • /**
     * @param {number[][]} points
     * @return {number}
     */
    var maxWidthOfVerticalArea = function (points) {
        points.sort((a, b) => a[0] - b[0]);
        let ans = 0;
        let px = points[0][0];
        for (const [x, _] of points) {
            ans = Math.max(ans, x - px);
            px = x;
        }
        return ans;
    };
    
    
  • function maxWidthOfVerticalArea(points: number[][]): number {
        const nums: number[] = points.map(point => point[0]);
        const inf = 1 << 30;
        const n = nums.length;
        let mi = inf;
        let mx = -inf;
        for (const x of nums) {
            mi = Math.min(mi, x);
            mx = Math.max(mx, x);
        }
        const bucketSize = Math.max(1, Math.floor((mx - mi) / (n - 1)));
        const bucketCount = Math.floor((mx - mi) / bucketSize) + 1;
        const buckets = new Array(bucketCount).fill(0).map(() => [inf, -inf]);
        for (const x of nums) {
            const i = Math.floor((x - mi) / bucketSize);
            buckets[i][0] = Math.min(buckets[i][0], x);
            buckets[i][1] = Math.max(buckets[i][1], x);
        }
        let prev = inf;
        let ans = 0;
        for (const [left, right] of buckets) {
            if (left > right) {
                continue;
            }
            ans = Math.max(ans, left - prev);
            prev = right;
        }
        return ans;
    }
    
    

All Problems

All Solutions