Formatted question description: https://leetcode.ca/all/1610.html

1610. Maximum Number of Visible Points (Medium)

You are given an array points, an integer angle, and your location, where location = [posx, posy] and points[i] = [xi, yi] both denote integral coordinates on the X-Y plane.

Initially, you are facing directly east from your position. You cannot move from your position, but you can rotate. In other words, posx and posy cannot be changed. Your field of view in degrees is represented by angle, determining how wide you can see from any given view direction. Let d be the amount in degrees that you rotate counterclockwise. Then, your field of view is the inclusive range of angles [d - angle/2, d + angle/2].

You can see some set of points if, for each point, the angle formed by the point, your position, and the immediate east direction from your position is in your field of view.

There can be multiple points at one coordinate. There may be points at your location, and you can always see these points regardless of your rotation. Points do not obstruct your vision to other points.

Return the maximum number of points you can see.

 

Example 1:

Input: points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]
Output: 3
Explanation: The shaded region represents your field of view. All points can be made visible in your field of view, including [3,3] even though [2,2] is in front and in the same line of sight.

Example 2:

Input: points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1]
Output: 4
Explanation: All points can be made visible in your field of view, including the one at your location.

Example 3:

Input: points = [[0,1],[2,1]], angle = 13, location = [1,1]
Output: 1
Explanation: You can only see one of the two points, as shown above.

 

Constraints:

  • 1 <= points.length <= 105
  • points[i].length == 2
  • location.length == 2
  • 0 <= angle < 360
  • 0 <= posx, posy, xi, yi <= 109

Related Topics:
Two Pointers, Geometry

Solution 1.

Iterate through the points array. For those which are the same as location, increment same; for others, store the corresponding degree in a vector<double> A.

We can use atan2(y, x) function to get the radian then turn the radien into degree.

Then we sort the A. We scan from the minimal degree towards the maximal. For each A[i], the ending degree is A[i] + angle, we can use binary search to count the degrees within [A[i], A[i] + angle].

One caveat is that the degree range is (-180, 180]. When A[i] + degree > 180, we need to find the points in (-180, A[i] + degree - 360] as well.

// OJ: https://leetcode.com/problems/maximum-number-of-visible-points/

// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
    int visiblePoints(vector<vector<int>>& P, int angle, vector<int>& L) {
        int same = 0, cnt = 0;
        vector<double> A;
        for (auto &p : P) {
            if (p == L) ++same;
            else A.push_back(atan2(p[1] - L[1], p[0] - L[0]) / M_PI * 180);
        }
        sort(begin(A), end(A));
        for (int i = 0; i < A.size(); ++i) {
            double bound = A[i] + angle;
            int j = upper_bound(begin(A), end(A), bound) - begin(A), total = j - i;
            if (bound > 180) total += upper_bound(begin(A), end(A), bound - 360) - begin(A);
            cnt = max(cnt, total);
        }
        return same + cnt;
    }
};

Solution 2. Two pointers

For the second loop, we can use two pointers to reduce the time complexity from O(NlogN) to O(N). But the overall time complexity is still O(NlogN) due to sorting.

// OJ: https://leetcode.com/problems/maximum-number-of-visible-points/

// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
    int visiblePoints(vector<vector<int>>& P, int angle, vector<int>& L) {
        int same = 0, cnt = 0;
        vector<double> A;
        for (auto &p : P) {
            if (p == L) ++same;
            else A.push_back(atan2(p[1] - L[1], p[0] - L[0]) / M_PI * 180);
        }
        sort(begin(A), end(A));
        for (int i = 0, j = 0, N = A.size(); i < N; ++i) {
            double bound = A[i] + angle;
            while (j < i + N && A[j % N] <= bound) ++j;
            A[i] += 360;
            cnt = max(cnt, j - i);
        }
        return same + cnt;
    }
};

Or

// OJ: https://leetcode.com/problems/maximum-number-of-visible-points/

// Time: O(NlogN)
// Space: O(N)
class Solution {
public:
    int visiblePoints(vector<vector<int>>& P, int angle, vector<int>& L) {
        int same = 0, cnt = 0;
        vector<double> A;
        for (auto &p : P) {
            if (p == L) ++same;
            else A.push_back(atan2(p[1] - L[1], p[0] - L[0]) / M_PI * 180);
        }
        sort(begin(A), end(A));
        int N = A.size();
        for (int i = 0; i < N; ++i) A.push_back(A[i] + 360);
        for (int i = 0, j = 0; i < N; ++i) {
            double bound = A[i] + angle;
            while (j < A.size() && A[j] <= bound) ++j;
            cnt = max(cnt, j - i);
        }
        return same + cnt;
    }
};

Java

class Solution {
    public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) {
        int visiblePoints = 0;
        int x = location.get(0), y = location.get(1);
        List<Double> pointAngles = new ArrayList<Double>();
        for (List<Integer> point : points) {
            int pointX = point.get(0) - x, pointY = point.get(1) - y;
            if (pointX == 0 && pointY == 0)
                visiblePoints++;
            else if (pointX == 0) {
                if (pointY > 0)
                    pointAngles.add(90.0);
                else
                    pointAngles.add(270.0);
            } else if (pointY == 0) {
                if (pointX > 0)
                    pointAngles.add(0.0);
                else
                    pointAngles.add(180.0);
            } else {
                double radian = Math.abs(Math.atan(1.0 * pointY / pointX));
                double degrees = Math.toDegrees(radian);
                if (pointX < 0 && pointY > 0)
                    degrees = 180 - degrees;
                else if (pointX < 0 && pointY < 0)
                    degrees = 180 + degrees;
                else if (pointX > 0 && pointY < 0)
                    degrees = 360 - degrees;
                pointAngles.add(degrees);
            }
        }
        Collections.sort(pointAngles);
        int maxCount = 0;
        int size = pointAngles.size();
        for (int i = 0; i < size; i++)
            pointAngles.add(pointAngles.get(i) + 360);
        size *= 2;
        int start = 0, end = 0;
        while (end < size) {
            double degrees = pointAngles.get(end);
            while (start < end && degrees - pointAngles.get(start) > angle)
                start++;
            end++;
            maxCount = Math.max(maxCount, end - start);
        }
        visiblePoints += maxCount;
        return visiblePoints;
    }
}

All Problems

All Solutions