给你一个点数组 points
和一个表示角度的整数 angle
,你的位置是 location
,其中 location = [posx, posy]
且 points[i] = [xi, yi]
都表示 X-Y 平面上的整数坐标。
最开始,你面向东方进行观测。你 不能 进行移动改变位置,但可以通过 自转 调整观测角度。换句话说,posx
和 posy
不能改变。你的视野范围的角度用 angle
表示, 这决定了你观测任意方向时可以多宽。设 d
为你逆时针自转旋转的度数,那么你的视野就是角度范围 [d - angle/2, d + angle/2]
所指示的那片区域。
对于每个点,如果由该点、你的位置以及从你的位置直接向东的方向形成的角度 位于你的视野中 ,那么你就可以看到它。
同一个坐标上可以有多个点。你所在的位置也可能存在一些点,但不管你的怎么旋转,总是可以看到这些点。同时,点不会阻碍你看到其他点。
返回你能看到的点的最大数目。
示例 1:
输入:points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1] 输出:3 解释:阴影区域代表你的视野。在你的视野中,所有的点都清晰可见,尽管 [2,2] 和 [3,3]在同一条直线上,你仍然可以看到 [3,3] 。
示例 2:
输入:points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1] 输出:4 解释:在你的视野中,所有的点都清晰可见,包括你所在位置的那个点。
示例 3:
输入:points = [[1,0],[2,1]], angle = 13, location = [1,1] 输出:1 解释:如图所示,你只能看到两点之一。
提示:
1 <= points.length <= 10^5
points[i].length == 2
location.length == 2
0 <= angle < 360
0 <= posx, posy, xi, yi <= 100
提示 1
Sort the points by polar angle with the original position. Now only a consecutive collection of points would be visible from any coordinate.
提示 2
We can use two pointers to keep track of visible points for each start point
提示 3
For handling the cyclic condition, it’d be helpful to append the point list to itself after sorting.
解法1:二分查找
double atan2( double y, double x );
atan2
返回 -π 到 π 弧度范围内的 y/x
的反正切值。 如果 atan2
的这两个参数均为 0,则函数返回 0。 所有结果都都以弧度为单位。
atan2
计算 y/x 的反正切值(假设 x
等于 0,如果 y
为正,则 atan2
返回 π/2;如果 y
为负,则返回 -π/2;如果 y
为 0,则返回 0)。
- 对于某些点的坐标(x,y)刚好处于 location 的元素, x == locationX && y == locationY 需要单独进行统计,因为当 atan2 的两个参数都为 0 时,atan2 的返回值是0,对应的弧度为 0,角度也为 0,但无论视角范围是多少,我们总能看到位于 location 的元素。
- 如果某些点的坐标 x != locationX && y == location Y,则 atan2 的返回值是0,对应的弧度为 0,角度也为 0,但如果此时我们的视角范围不包括 0 degree,我们看不到这个点。
- 综上所述,x == locationX && y == locationY 的点坐标 不需要计算 atan2 值,需要单独统计。
由于 atan2
的返回值范围是 [-π, π],为了方便统计,我们把所有角的弧度转为 [0, 2π],
也可转为角度[0, 360],我们将所有点(除了位于location的点)坐标转换成极角并存储到 列表 polarDegrees, 然后对列表 polarDegrees 排序。
我们的视角范围为 [start, end], end = start + angle,
如果 end <= 360 degree or end <= 2π, 则视角范围为 [start, end],
如果 end > 360 degree or end > 2π, 则 end -= 360 or end -= 2π,视角范围为 [start, 360] [0, end]
or [start, 2π] [0, end],由于列表 polarDegrees 有序,则我们只需计算角度在[0, end]范围内的个数
写法1:角度 degrees
class Solution {
public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) {
int locationX = location.get(0);
int locationY = location.get(1);
List<Double> polarDegrees = new ArrayList<>();
int self = 0;
int ans = 0;
for (List<Integer> point : points) {
int x = point.get(0);
int y = point.get(1);
if (x == locationX && y == locationY) {
self++;
continue;
}
double angleDegrees = Math.toDegrees(Math.atan2(y - locationY, x - locationX));
if (angleDegrees < 0) {
angleDegrees += 360.0;
}
polarDegrees.add(angleDegrees);
}
Collections.sort(polarDegrees);
int m = polarDegrees.size();
for (int i = 0; i < m; i++) {
double start = polarDegrees.get(i);
double end = start + angle;
if (end > 360.0) {
end -= 360.0;
}
ans = Math.max(ans, binarySearch(polarDegrees, start, end, i, m));
}
return ans + self;
}
private int binarySearch(List<Double> polarDegrees, double start, double end, int i, int m) {
int len = 0;
// start <= end,则角度范围:[start, end],由于polarDegrees是有序的,所以只要找到小于end的最大下标即可
int left = i;
int right = m - 1;
// start > end,则角度范围:[start, 360] | [0, end],由于polarDegrees是有序的,所以只要找到小于end的最大下标即可
if (start > end) {
left = 0;
right = i;
}
while (left <= right) {
// 循环不变量:
// polarDegrees.get(left - 1) <= end
// polarDegrees.get(right + 1) > end
int mid = left + (right - left) / 2;
if (polarDegrees.get(mid) <= end) {
left = mid + 1;
} else {
right = mid - 1;
}
}
if (start <= end) {
len = left - i;
} else {
len = left + m - i;
}
return len;
}
}
复杂度分析
- 时间复杂度:O( n log n)。其中 n 为坐标的个数 即列表 points 的长度,m 为列表 polarDegrees 的长度,最坏情况下 m == n。由于需要对所有的点坐标(x, y)转化成到原点 (locationX, locationY) 的极角,再对极角进行排序,再对每一个坐标的区间进行二分查找,因此总的时间复杂度应该为 O(n + m log m + mlog m) = O( m log m) = O( n log n)。
- 空间复杂度:O(n)。
写法2:弧度 radians
class Solution {
public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) {
int locationX = location.get(0);
int locationY = location.get(1);
List<Double> polarRadians = new ArrayList<>();
int self = 0;
int ans = 0;
double PI2 = 2.0 * Math.PI;
for (List<Integer> point : points) {
int x = point.get(0);
int y = point.get(1);
if (x == locationX && y == locationY) {
self++;
continue;
}
double angleRadians = Math.atan2(y - locationY, x - locationX);
if (angleRadians < 0) {
angleRadians += PI2;
}
polarRadians.add(angleRadians);
}
Collections.sort(polarRadians);
int m = polarRadians.size();
for (int i = 0; i < m; i++) {
double start = polarRadians.get(i);
double end = start + Math.toRadians(angle);
if (end > PI2) {
end -= PI2;
}
ans = Math.max(ans, binarySearch(polarRadians, start, end, i, m));
}
return ans + self;
}
private int binarySearch(List<Double> polarRadians, double start, double end, int i, int m) {
int len = 0;
// start <= end,则角度范围:[start, end],由于polarDegrees是有序的,所以只要找到小于end的最大下标即可
int left = i;
int right = m - 1;
// start > end,则角度范围:[start, 360] | [0, end],由于polarDegrees是有序的,所以只要找到小于end的最大下标即可
if (start > end) {
left = 0;
right = i;
}
while (left <= right) {
// 循环不变量:
// polarRadians.get(left - 1) <= end
// polarRadians.get(right + 1) > end
int mid = left + (right - left) / 2;
if (polarRadians.get(mid) <= end) {
left = mid + 1;
} else {
right = mid - 1;
}
}
if (start <= end) {
len = left - i;
} else {
len = left + m - i;
}
return len;
}
}
复杂度分析
- 时间复杂度:O( n log n)。其中 n 为坐标的个数 即列表 points 的长度,m 为列表 polarRadinas 的长度,最坏情况下 m == n。由于需要对所有的点坐标(x, y)转化成到原点 (locationX, locationY) 的极角,再对极角进行排序,再对每一个坐标的区间进行二分查找,因此总的时间复杂度应该为 O(n + m log m + mlog m) = O( m log m) = O( n log n)。
- 空间复杂度:O(n)。
解法2:滑动窗口
class Solution {
public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) {
int locationX = location.get(0);
int locationY = location.get(1);
List<Double> polarRadians = new ArrayList<>();
int self = 0;
int ans = 0;
double PI2 = 2.0 * Math.PI;
for (List<Integer> point : points) {
int x = point.get(0);
int y = point.get(1);
if (x == locationX && y == locationY) {
self++;
continue;
}
double angleRadians = Math.atan2(y - locationY, x - locationX);
if (angleRadians < 0) {
angleRadians += PI2;
}
polarRadians.add(angleRadians);
}
Collections.sort(polarRadians);
int m = polarRadians.size();
// 因为角度成环问题,radians == radians + 2*PI 所以先将所有角加上2*PI 再添加到原列表末尾
polarRadians.addAll(polarRadians.stream().map(i -> i + PI2).collect(Collectors.toList()));
int left = 0;
int right = 0;
double radians = Math.toRadians(angle);
while (right < 2 * m) {
while (polarRadians.get(right) > polarRadians.get(left) + radians) {
left++;
}
ans = Math.max(ans, right - left + 1);
right++;
}
return ans + self;
}
}
复杂度分析
- 时间复杂度:O(n log n),其中 n 为坐标的个数,总的时间复杂度应该为 O(n + n log n + 2n ) = O( n log n)。
- 空间复杂度:O(n),其中 n 为坐标的个数,我们总共最多需要两倍坐标个数的空间来存储坐标的极角。