forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1610
No.1610.Maximum Number of Visible Points
- Loading branch information
Showing
6 changed files
with
335 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
solution/1600-1699/1610.Maximum Number of Visible Points/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Solution { | ||
public: | ||
int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) { | ||
vector<double> v; | ||
int x = location[0], y = location[1]; | ||
int same = 0; | ||
for (auto& p : points) | ||
{ | ||
int xi = p[0], yi = p[1]; | ||
if (xi == x && yi == y) ++same; | ||
else v.emplace_back(atan2(yi - y, xi - x)); | ||
} | ||
sort(v.begin(), v.end()); | ||
int n = v.size(); | ||
for (int i = 0; i < n; ++i) v.emplace_back(v[i] + 2 * M_PI); | ||
|
||
int mx = 0; | ||
double t = angle * M_PI / 180; | ||
for (int i = 0, j = 0; j < 2 * n; ++j) | ||
{ | ||
while (i < j && v[j] - v[i] > t) ++i; | ||
mx = max(mx, j - i + 1); | ||
} | ||
return mx + same; | ||
} | ||
}; |
32 changes: 32 additions & 0 deletions
32
solution/1600-1699/1610.Maximum Number of Visible Points/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
func visiblePoints(points [][]int, angle int, location []int) int { | ||
same := 0 | ||
v := []float64{} | ||
for _, p := range points { | ||
if p[0] == location[0] && p[1] == location[1] { | ||
same++ | ||
} else { | ||
v = append(v, math.Atan2(float64(p[1]-location[1]), float64(p[0]-location[0]))) | ||
} | ||
} | ||
sort.Float64s(v) | ||
for _, deg := range v { | ||
v = append(v, deg+2*math.Pi) | ||
} | ||
|
||
mx := 0 | ||
t := float64(angle) * math.Pi / 180 | ||
for i, j := 0, 0; j < len(v); j++ { | ||
for i < j && v[j]-v[i] > t { | ||
i++ | ||
} | ||
mx = max(mx, j-i+1) | ||
} | ||
return same + mx | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} |
29 changes: 29 additions & 0 deletions
29
solution/1600-1699/1610.Maximum Number of Visible Points/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Solution { | ||
public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) { | ||
List<Double> v = new ArrayList<>(); | ||
int x = location.get(0), y = location.get(1); | ||
int same = 0; | ||
for (List<Integer> p : points) { | ||
int xi = p.get(0), yi = p.get(1); | ||
if (xi == x && yi == y) { | ||
++same; | ||
continue; | ||
} | ||
v.add(Math.atan2(yi - y, xi - x)); | ||
} | ||
Collections.sort(v); | ||
int n = v.size(); | ||
for (int i = 0; i < n; ++i) { | ||
v.add(v.get(i) + 2 * Math.PI); | ||
} | ||
int mx = 0; | ||
Double t = angle * Math.PI / 180; | ||
for (int i = 0, j = 0; j < 2 * n; ++j) { | ||
while (i < j && v.get(j) - v.get(i) > t) { | ||
++i; | ||
} | ||
mx = Math.max(mx, j - i + 1); | ||
} | ||
return mx + same; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
solution/1600-1699/1610.Maximum Number of Visible Points/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
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 + same |