leetcode 1610. Maximum Number of Visible Points(python)

語言: CN / TW / HK

「這是我參與11月更文挑戰的第23天,活動詳情檢視:2021最後一次更文挑戰

描述

You are given an array points, an integer angle, and your location, where location = [posx, posy] and points[i] = [xi, yi] both denote integral coordinates on the X-Y plane.

Initially, you are facing directly east from your position. You cannot move from your position, but you can rotate. In other words, posx and posy cannot be changed. Your field of view in degrees is represented by angle, determining how wide you can see from any given view direction. Let d be the amount in degrees that you rotate counterclockwise. Then, your field of view is the inclusive range of angles [d - angle/2, d + angle/2].

You can see some set of points if, for each point, the angle formed by the point, your position, and the immediate east direction from your position is in your field of view.

There can be multiple points at one coordinate. There may be points at your location, and you can always see these points regardless of your rotation. Points do not obstruct your vision to other points.

Return the maximum number of points you can see.

Example 1:

Input: points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]
Output: 3
Explanation: The shaded region represents your field of view. All points can be made visible in your field of view, including [3,3] even though [2,2] is in front and in the same line of sight.

Example 2:

Input: points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1]
Output: 4
Explanation: All points can be made visible in your field of view, including the one at your location.

Example 3:

Input: points = [[1,0],[2,1]], angle = 13, location = [1,1]
Output: 1
Explanation: You can only see one of the two points, as shown above.

Note:

  • 1 <= points.length <= 10^5
  • points[i].length == 2
  • location.length == 2
  • 0 <= angle < 360
  • 0 <= posx, posy, xi, yi <= 100

解析

根據題意,給定一個列表 points 、一個整數 angle 和您的 location ,其中 location = [posx, posy] 和 points[i] = [xi, yi] 都表示 X-Y 平面上的積分座標。

最初,站您的位置直接面向東。你不能從你的位置移動,但你可以旋轉。換句話說,posx 和 posy 不能改變。以度為單位的視野由 angle 表示,決定了從任何給定的視線方向可以看到的寬度。讓 d 表示您逆時針旋轉的度數。然後,您的視野是角度 [d - angle/2, d + angle/2] 的包含範圍。

如果對於每個點,由該點、您的位置和從您的位置直接向東形成的角度在您的視野中,您就可以看到一些點集合。一個座標上可以有多個點。您所在的位置可能有一些點,無論您如何旋轉,您始終可以看到這些點。點不會妨礙您對其他點的視線。返回您可以看到的最大點數。

其實這道題讀完之後也比較簡單,總體思路就是將 location 看成原點,然後計算 points 中的每個點和原點形成的夾角角度,都記錄到 angles 中,然後使用滑動視窗思想找在 angle 範圍內點數最多的點。題目中有兩個點需要格外注意:

  • 第一個是計算夾角,需要注意範圍為 [0, 2*pi] ,但是可能在 0 度附近可能上下都有符合題意的角度存在,所以需要將 angles 後再擴充其每個元素加 2*pi 的值,可以表示 [2*pi, 4*pi] 的範圍
  • 第二個就是小數的精度問題,必須要考慮到位,畢竟計算機在計算夾角的時候是浮點數,精度不夠可能找點數會出現誤差
  • 第三個就是有些點就在原點,這些點是始終都能被看到的,直接加到結果即可

解答

class Solution(object):
    def visiblePoints(self, points, angle, location):
        """
        :type points: List[List[int]]
        :type angle: int
        :type location: List[int]
        :rtype: int
        """
        angles = []
        pi = 3.1415926
        origin = 0
        for x, y in points:
            dx = x - location[0]
            dy = y - location[1]
            if dx == 0 and dy == 0:
                origin += 1
                continue
            alpha = atan2(dy, dx) + pi
            angles.append(alpha)
        angles.sort()
        N = len(angles)
        for i in range(N):
            angles.append(angles[i] + 2 * pi)

        result = j = 0
        for i in range(2*N):
            while j < 2 * N and angles[j] - angles[i] <= angle * 1.0 * pi / 180 + 0.0000001:
                j += 1
            result = max(result, j - i)
        return result + origin

執行結果

Runtime: 2204 ms, faster than 43.70% of Python online submissions for Maximum Number of Visible Points.
Memory Usage: 56.3 MB, less than 36.97% of Python online submissions for Maximum Number of Visible Points.

原題連結:https://leetcode.com/problems/maximum-number-of-visible-points/

您的支援是我最大的動力