Skip to content
Rain Hu's Workspace
Go back

[LeetCode] 1610. Maximum Number of Visible Points

Rain Hu
class Solution {
public:
    int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) {
        vector<double> angles;
        int on = 0;
        for (const auto& p : points) {
            int dy = p[1] - location[1];
            int dx = p[0] - location[0];
            if (dx == 0 && dy == 0) {
                on++;
                continue;
            }
            double ang = atan2(dy, dx) * 180 / std::numbers::pi;
            if (ang < 180 + angle) angles.push_back(ang + 360);
            angles.push_back(ang);
        }
        sort(angles.begin(), angles.end());
        int left = 0, right = 0, res = 0, n = angles.size();
        while (right < n) {
            double curr = angles[right++];
            while (curr - angles[left] > angle) left++;
            res = max(res, right - left);
        }
        return res + on;
    }
};

Share this post on:

Previous
[LeetCode] 433. Minimum Genetic Mutation
Next
[LeetCode] 2009. Minimum Number of Operations to Make Array Continuous