|
57 | 57 | <li><code>0 <= pos<sub>x</sub>, pos<sub>y</sub>, x<sub>i</sub>, y<sub>i</sub> <= 100</code></li>
|
58 | 58 | </ul>
|
59 | 59 |
|
60 |
| - |
61 | 60 | ## 解法
|
62 | 61 |
|
63 | 62 | <!-- 这里可写通用的实现逻辑 -->
|
64 | 63 |
|
| 64 | +根据题目我们得知,需要求出在视角范围 `[d - angle/2, d + angle / 2]` 范围内覆盖的最多点的数量。视角可以转换为相对于 location `(x, y)` 的极角。 |
| 65 | + |
| 66 | +可以排除与 location 重合的点,将剩下的所有点 p 的坐标 `(xi, yi)` 转换为相对于 `(x, y)` 的极角。可以利用 `atan2` 函数,`atan2` 返回值范围是 `[−π,π]`,覆盖范围是 2π。 |
| 67 | + |
| 68 | +求出极角后,按照大小进行排序。因为可以循环,所以把整个数组所有元素加上 2π 接在数组后面。 |
| 69 | + |
| 70 | +接下来利用双指针找出覆盖最多点的区间即可。最后返回时,要把重合的点加上。 |
| 71 | + |
65 | 72 | <!-- tabs:start -->
|
66 | 73 |
|
67 | 74 | ### **Python3**
|
68 | 75 |
|
69 | 76 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
70 | 77 |
|
71 | 78 | ```python
|
72 |
| - |
| 79 | +class Solution: |
| 80 | + def visiblePoints(self, points: List[List[int]], angle: int, location: List[int]) -> int: |
| 81 | + v = [] |
| 82 | + x, y = location |
| 83 | + same = 0 |
| 84 | + for xi, yi in points: |
| 85 | + if xi == x and yi == y: |
| 86 | + same += 1 |
| 87 | + else: |
| 88 | + v.append(atan2(yi - y, xi - x)) |
| 89 | + v.sort() |
| 90 | + n = len(v) |
| 91 | + v += [deg + 2 * pi for deg in v] |
| 92 | + t = angle * pi / 180 |
| 93 | + mx = max((bisect_right(v, v[i] + t) - i for i in range(n)), default=0) |
| 94 | + return mx + same |
73 | 95 | ```
|
74 | 96 |
|
75 | 97 | ### **Java**
|
76 | 98 |
|
77 | 99 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
78 | 100 |
|
79 | 101 | ```java
|
| 102 | +class Solution { |
| 103 | + public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) { |
| 104 | + List<Double> v = new ArrayList<>(); |
| 105 | + int x = location.get(0), y = location.get(1); |
| 106 | + int same = 0; |
| 107 | + for (List<Integer> p : points) { |
| 108 | + int xi = p.get(0), yi = p.get(1); |
| 109 | + if (xi == x && yi == y) { |
| 110 | + ++same; |
| 111 | + continue; |
| 112 | + } |
| 113 | + v.add(Math.atan2(yi - y, xi - x)); |
| 114 | + } |
| 115 | + Collections.sort(v); |
| 116 | + int n = v.size(); |
| 117 | + for (int i = 0; i < n; ++i) { |
| 118 | + v.add(v.get(i) + 2 * Math.PI); |
| 119 | + } |
| 120 | + int mx = 0; |
| 121 | + Double t = angle * Math.PI / 180; |
| 122 | + for (int i = 0, j = 0; j < 2 * n; ++j) { |
| 123 | + while (i < j && v.get(j) - v.get(i) > t) { |
| 124 | + ++i; |
| 125 | + } |
| 126 | + mx = Math.max(mx, j - i + 1); |
| 127 | + } |
| 128 | + return mx + same; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### **C++** |
| 134 | + |
| 135 | +```cpp |
| 136 | +class Solution { |
| 137 | +public: |
| 138 | + int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) { |
| 139 | + vector<double> v; |
| 140 | + int x = location[0], y = location[1]; |
| 141 | + int same = 0; |
| 142 | + for (auto& p : points) |
| 143 | + { |
| 144 | + int xi = p[0], yi = p[1]; |
| 145 | + if (xi == x && yi == y) ++same; |
| 146 | + else v.emplace_back(atan2(yi - y, xi - x)); |
| 147 | + } |
| 148 | + sort(v.begin(), v.end()); |
| 149 | + int n = v.size(); |
| 150 | + for (int i = 0; i < n; ++i) v.emplace_back(v[i] + 2 * M_PI); |
| 151 | + |
| 152 | + int mx = 0; |
| 153 | + double t = angle * M_PI / 180; |
| 154 | + for (int i = 0, j = 0; j < 2 * n; ++j) |
| 155 | + { |
| 156 | + while (i < j && v[j] - v[i] > t) ++i; |
| 157 | + mx = max(mx, j - i + 1); |
| 158 | + } |
| 159 | + return mx + same; |
| 160 | + } |
| 161 | +}; |
| 162 | +``` |
80 | 163 |
|
| 164 | +### **Go** |
| 165 | + |
| 166 | +```go |
| 167 | +func visiblePoints(points [][]int, angle int, location []int) int { |
| 168 | + same := 0 |
| 169 | + v := []float64{} |
| 170 | + for _, p := range points { |
| 171 | + if p[0] == location[0] && p[1] == location[1] { |
| 172 | + same++ |
| 173 | + } else { |
| 174 | + v = append(v, math.Atan2(float64(p[1]-location[1]), float64(p[0]-location[0]))) |
| 175 | + } |
| 176 | + } |
| 177 | + sort.Float64s(v) |
| 178 | + for _, deg := range v { |
| 179 | + v = append(v, deg+2*math.Pi) |
| 180 | + } |
| 181 | + |
| 182 | + mx := 0 |
| 183 | + t := float64(angle) * math.Pi / 180 |
| 184 | + for i, j := 0, 0; j < len(v); j++ { |
| 185 | + for i < j && v[j]-v[i] > t { |
| 186 | + i++ |
| 187 | + } |
| 188 | + mx = max(mx, j-i+1) |
| 189 | + } |
| 190 | + return same + mx |
| 191 | +} |
| 192 | + |
| 193 | +func max(a, b int) int { |
| 194 | + if a > b { |
| 195 | + return a |
| 196 | + } |
| 197 | + return b |
| 198 | +} |
81 | 199 | ```
|
82 | 200 |
|
83 | 201 | ### **...**
|
|
0 commit comments