the second one in Biweekly Contest 38.
Difficulty : Medium
Related Topics : Sort
Given n
points
on a 2D plane wherepoints[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.
Input: points = [[8,7],[9,9],[7,4],[9,7]] Output: 1 Explanation: Both the red and the blue area are optimal.
Input: points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]] Output: 3
n == points.length
2 <= n <= 10^5
points[i].length == 2
0 <= xi, yi <= 10^9
- mine
- Java
Runtime: 37 ms, faster than 74.04%, Memory Usage: 67.3 MB, less than 5.78% of Java online submissions
// O(N*logN)time // O(1)space public int maxWidthOfVerticalArea(int[][] points) { Arrays.sort(points, (p1, p2) -> p1[0] - p2[0]); int res = 0; for(int i = 1; i < points.length; i++){ res = Math.max(res, points[i][0] - points[i - 1][0]); } return res; }
- Java