Skip to content

Latest commit

 

History

History
64 lines (49 loc) · 1.67 KB

File metadata and controls

64 lines (49 loc) · 1.67 KB

1637. Widest Vertical Area Between Two Points Containing No Points

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

Solutions (Ruby)

1. Sort

# @param {Integer[][]} points
# @return {Integer}
def max_width_of_vertical_area(points)
  ret = 0

  points.sort_by! { |p| p[0] }

  (1...points.length).each do |i|
    ret = [ret, points[i][0] - points[i - 1][0]].max
  end

  ret
end

Solutions (Rust)

1. Sort

impl Solution {
    pub fn max_width_of_vertical_area(mut points: Vec<Vec<i32>>) -> i32 {
        let mut ret = 0;

        points.sort_unstable_by_key(|p| p[0]);

        for i in 1..points.len() {
            ret = ret.max(points[i][0] - points[i - 1][0]);
        }

        ret
    }
}