1637. The widest vertical area that does not contain any points between two points

Directory link:

Lituo Programming Problems - Summary of Solutions_Share+Records-CSDN Blog

GitHub synchronous brushing project:

https://github.com/September26/java-algorithms

Link to the original title: Likou


describe:

Given  n a point on a two-dimensional plane  points ,  points[i] = [xi, yi] please return  the width of the widest vertical area between two points that does not contain any points inside  .

A vertical area  is defined as an area of ​​fixed width that extends infinitely on the y-axis (that is, the height is infinite). The widest vertical area  is a vertical area with the largest width.

Note that   points  on the edge of the vertical region are not  inside the region.

Example 1:

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

Example 2:

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

hint:

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

Problem-solving ideas:

* Problem-solving ideas:
* This question is a bit inexplicable, just sort the x-axis directly and find the maximum value between the two.

code:

public class Solution1637 {

    public int maxWidthOfVerticalArea(int[][] points) {
        List<Integer> collect = Arrays.stream(points).map(ints -> ints[0]).sorted(Comparator.comparingInt(o -> o)).collect(Collectors.toList());
        int abs = 0;
        for (int i = 1; i < collect.size(); i++) {
            abs = Math.max(abs, collect.get(i) - collect.get(i - 1));
        }
        return abs;
    }
}

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/129851244