-
Notifications
You must be signed in to change notification settings - Fork 0
/
WidestVerticalArea.java
35 lines (27 loc) · 1.2 KB
/
WidestVerticalArea.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//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:
//Input: points = [[8,7],[9,9],[7,4],[9,7]]
// Output: 1
// Explanation: Both the red and the blue area are optimal.
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.stream.Collectors;
public class WidestVerticalArea {
public static int maxWidthOfVerticalArea(int[][] points) {
Integer[] xPoints=new Integer[points.length];
for (int i = 0; i < points.length; i++) {
xPoints[i]=points[i][0];
}
Arrays.sort(xPoints);
for (int i = 0; i <xPoints.length-1 ; i++) {
xPoints[i]=xPoints[i+1]-xPoints[i];
}
xPoints[xPoints.length-1]=0;
return Collections.max(Arrays.asList(xPoints));
}
}