Skip to content

Commit cd29d7d

Browse files
authored
Create maximum-number-of-visible-points.py
1 parent 93bf412 commit cd29d7d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
import math
5+
6+
7+
class Solution(object):
8+
def visiblePoints(self, points, angle, location):
9+
"""
10+
:type points: List[List[int]]
11+
:type angle: int
12+
:type location: List[int]
13+
:rtype: int
14+
"""
15+
arr, extra = [], 0
16+
for p in points:
17+
if p == location:
18+
extra += 1
19+
continue
20+
arr.append(math.atan2(p[1]-location[1], p[0]-location[0]))
21+
arr.sort()
22+
arr.extend([x + 2.0*math.pi for x in arr]) # make it circular
23+
d = 2.0*math.pi * (angle/360.0)
24+
left = result = 0
25+
for right in xrange(len(arr)):
26+
while arr[right]-arr[left] > d:
27+
left += 1
28+
result = max(result, right-left+1)
29+
return result + extra

0 commit comments

Comments
 (0)