Skip to content

Commit

Permalink
Merge pull request #4869 from freakin23/master
Browse files Browse the repository at this point in the history
correct C++ code / py sol for Maximum Number of Visible Points
  • Loading branch information
SansPapyrus683 authored Oct 27, 2024
2 parents 4f0f151 + c41535e commit 816913a
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions content/5_Plat/Geo_Pri.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ points from the beginning of the sorted array, thus the array of angles should b
## Implementation
**Time Complexity:** $\mathcal{O}(N)$
**Time Complexity:** $\mathcal{O}(N \log N)$
<LanguageSection>
<CPPSection>
Expand All @@ -514,7 +514,9 @@ class Solution {
angles.push_back(atan2(p[1] - location[1], p[0] - location[0]));
}

// Sort agngles
if (angles.empty()) { return extra; }

// Sort angles
sort(angles.begin(), angles.end());
// Duplicate the array and add 2*PI to the angles
for (int i = 0; i < (int)points.size(); i++) {
Expand All @@ -536,6 +538,49 @@ class Solution {
```
</CPPSection>
<PySection>
```py
class Solution:
def visiblePoints(
self, points: List[List[int]], angle: int, location: List[int]
) -> int:
extra = 0
angles = []

for p in points:
# Ignore points identical to location
if p == location:
extra += 1
continue

# Take the arctg in radians
angles.append(atan2(p[1] - location[1], p[0] - location[0]))

if not angles:
return extra

# Sort angles
angles.sort()

# Duplicate the array and add 2*PI to the angles
angles += [a + 2 * pi for a in angles]

ans = 0
angle_radians = pi * angle / 180

l = 0
# Sliding window
for r in range(len(angles)):
# Pop all the points out of the field of view
while angles[r] - angles[l] > angle_radians:
l += 1
ans = max(ans, r - l + 1)

return ans + extra
```
</PySection>
</LanguageSection>
Expand Down

0 comments on commit 816913a

Please sign in to comment.