leetcode 1637. Widest Vertical Area Between Two Points Containing No Points(pyt

448 阅读1分钟

描述

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

Note:

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

解析

根据题意,其实只需要找到最大的宽度就可以了,只和每个 points[i][0] 有关系,先把 points[i][0] (其实就是所有的 x 坐标)都提出来组成数组排序,然后计算前后相邻的两个 x 坐标的宽度,只需要找出最大的宽度即可。

解答

class Solution(object):
    def maxWidthOfVerticalArea(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        points = [p[0] for p in points]
        points.sort()
        res = float("-inf")
        for i in range(1,len(points)):
            a = points[i]
            b = points[i-1]
            res = max(a-b, res)
        return res
        	      
		

运行结果

Runtime: 700 ms, faster than 95.34% of Python online submissions for Widest Vertical Area Between Two Points Containing No Points.
Memory Usage: 57.3 MB, less than 38.86% of Python online submissions for Widest Vertical Area Between Two Points Containing No Points.

原题链接:leetcode.com/problems/wi…

您的支持是我最大的动力