Given npoints 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.

Input: points =[[8,7],[9,9],[7,4],[9,7]]Output: 1Explanation: Both the red and the blue area are optimal.
classSolution {
public:int maxWidthOfVerticalArea(vector<vector<int>>& points) {
vector<int> xs;
for (auto& p : points) xs.push_back(p[0]);
sort(xs.begin(), xs.end());
int ans =0;
for (int i =1; i < xs.size(); ++i) {
ans = max(ans, xs[i] - xs[i-1]);
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import"sort"funcmaxWidthOfVerticalArea(points [][]int) int {
xs:= make([]int, len(points))
fori, p:=rangepoints {
xs[i] = p[0]
}
sort.Ints(xs)
ans:=0fori:=1; i < len(xs); i++ {
ifxs[i]-xs[i-1] > ans {
ans = xs[i] -xs[i-1]
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.*;
classSolution {
publicintmaxWidthOfVerticalArea(int[][] points) {
int n = points.length;
int[] xs =newint[n];
for (int i = 0; i < n; i++) xs[i]= points[i][0];
Arrays.sort(xs);
int ans = 0;
for (int i = 1; i < n; i++) ans = Math.max(ans, xs[i]- xs[i-1]);
return ans;
}
}
1
2
3
4
5
6
classSolution {
funmaxWidthOfVerticalArea(points: Array<IntArray>): Int {
val xs = points.map { it[0] }.sorted()
return xs.zipWithNext { a, b -> b - a }.maxOrNull() ?:0 }
}
1
2
3
4
5
from typing import List
classSolution:
defmaxWidthOfVerticalArea(self, points: List[List[int]]) -> int:
xs = sorted(p[0] for p in points)
return max(xs[i] - xs[i-1] for i in range(1, len(xs)))