2023-03-30 LeetCode:1637. 两点之间不包

2023-03-29  本文已影响0人  alex很累

1637. 两点之间不包含任何点的最宽垂直区域

问题描述

给你 n 个二维平面上的点 points ,其中 points[i] = [xi, yi] ,请你返回两点之间内部不包含任何点的 最宽垂直区域 的宽度。
垂直区域 的定义是固定宽度,而 y 轴上无限延伸的一块区域(也就是高度为无穷大)。 最宽垂直区域 为宽度最大的一个垂直区域。
请注意,垂直区域边上 的点 不在 区域内。

示例

解题思路

points按照x轴位置进行排序,计算相邻坐标点的最宽间距。

代码示例(JAVA)

class Solution {
    public int maxWidthOfVerticalArea(int[][] points) {
        Arrays.sort(points, (a, b) -> a[0] - b[0]);
        int max = 0;
        for (int i = 0; i < points.length - 1; i++) {
            int width = points[i + 1][0] - points[i][0];
            max = Math.max(max, width);
        }

        return max;
    }
}

时间复杂度:O(n * logn)

上一篇 下一篇

猜你喜欢

热点阅读