1610. Maximum Number of Visible Points LeetCode Solution

In this guide, you will get 1610. Maximum Number of Visible Points LeetCode Solution with the best time and space complexity. The solution to Maximum Number of Visible Points problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Number of Visible Points solution in C++
  4. Maximum Number of Visible Points solution in Java
  5. Maximum Number of Visible Points solution in Python
  6. Additional Resources
1610. Maximum Number of Visible Points LeetCode Solution image

Problem Statement of Maximum Number of Visible Points

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].

Your browser does not support the video tag or this video format.

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 = [[1,0],[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 <= 100

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(n)

1610. Maximum Number of Visible Points LeetCode Solution in C++

class Solution {
 public:
  int visiblePoints(vector<vector<int>>& points, int angle,
                    vector<int>& location) {
    const int posX = location[0];
    const int posY = location[1];
    int maxVisible = 0;
    int same = 0;
    vector<double> pointAngles;

    for (const vector<int>& p : points) {
      const int x = p[0];
      const int y = p[1];
      if (x == posX && y == posY)
        ++same;
      else
        pointAngles.push_back(getAngle(y - posY, x - posX));
    }

    ranges::sort(pointAngles);

    const int n = pointAngles.size();
    for (int i = 0; i < n; ++i)
      pointAngles.push_back(pointAngles[i] + 360);

    for (int l = 0, r = 0; r < pointAngles.size(); ++r) {
      while (pointAngles[r] - pointAngles[l] > angle)
        ++l;
      maxVisible = max(maxVisible, r - l + 1);
    }

    return maxVisible + same;
  }

 private:
  double getAngle(int dy, int dx) {
    return atan2(dy, dx) * 180 / M_PI;
  }
};
/* code provided by PROGIEZ */

1610. Maximum Number of Visible Points LeetCode Solution in Java

class Solution {
  public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) {
    final int posX = location.get(0);
    final int posY = location.get(1);
    int maxVisible = 0;
    int same = 0;
    List<Double> pointAngles = new ArrayList<>();

    for (List<Integer> p : points) {
      final int x = p.get(0);
      final int y = p.get(1);
      if (x == posX && y == posY)
        ++same;
      else
        pointAngles.add(getAngle(y - posY, x - posX));
    }

    Collections.sort(pointAngles);

    final int n = pointAngles.size();
    for (int i = 0; i < n; ++i)
      pointAngles.add(pointAngles.get(i) + 360);

    for (int l = 0, r = 0; r < pointAngles.size(); ++r) {
      while (pointAngles.get(r) - pointAngles.get(l) > angle)
        ++l;
      maxVisible = Math.max(maxVisible, r - l + 1);
    }

    return maxVisible + same;
  }

  private double getAngle(int dy, int dx) {
    return Math.atan2(dy, dx) * 180 / Math.PI;
  }
}
// code provided by PROGIEZ

1610. Maximum Number of Visible Points LeetCode Solution in Python

class Solution:
  def visiblePoints(
      self,
      points: list[list[int]],
      angle: int,
      location: list[int],
  ) -> int:
    posX, posY = location
    maxVisible = 0
    same = 0
    A = []

    for x, y in points:
      if x == posX and y == posY:
        same += 1
      else:
        A.append(math.atan2(y - posY, x - posX))

    A.sort()
    A = A + [a + 2.0 * math.pi for a in A]

    angleInRadians = math.pi * (angle / 180)

    l = 0
    for r in range(len(A)):
      while A[r] - A[l] > angleInRadians:
        l += 1
      maxVisible = max(maxVisible, r - l + 1)

    return maxVisible + same
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.