Maximum Number of Visible Points — LeetCode 1610 Python Solution
- Problem
- #1610
- Pattern
- Sliding Window
- Reading time
- 3 min
- Source
- leetcode.com
The problem
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.
Example
- 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.
Python solution
class Solution:
def visiblePoints(
self, points: List[List[int]], angle: int, location: List[int]
) -> int:
v = []
x, y = location
same = 0
for xi, yi in points:
if xi == x and yi == y:
same += 1
else:
v.append(atan2(yi - y, xi - x))
v.sort()
n = len(v)
v += [deg + 2 * pi for deg in v]
t = angle * pi / 180
mx = max((bisect_right(v, v[i] + t) - i for i in range(n)), default=0)
return mx + sameComplexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) to O(n) auxiliary |
Pattern: Sliding Window
Collapse a nested loop over every subarray into a single pass with two indices. LeetCode 1610. Maximum Number of Visible Points is filed here because LeetCode tags it Sliding Window, which is the vocabulary this hub collects.
The sliding window guide has the Python template for the pattern and the 116 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1610. Maximum Number of Visible Points?
- LeetCode 1610. Maximum Number of Visible Points is rated Hard on LeetCode.
- What topics does LeetCode 1610. Maximum Number of Visible Points cover?
- LeetCode 1610. Maximum Number of Visible Points is tagged Geometry, Array, Math, Sorting and Sliding Window on LeetCode.