ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[Google] LeetCode 1610 Maximum Number of Visible Points 极角排序

2022-08-23 03:00:24  阅读:190  来源: 互联网

标签:Google angle int Number 极角 location view your 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].

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.

Solution

给定一个坐标,以及其他点,问给定视角,最多能看到多少个点。

以 \(location\) 为原点,计算出其他点与其的夹角。这里使用 \(cpp\) 的 \(atan2\) 得到反正切。

得到夹角以后,进行排序。注意到这是一个圈,所以我们同时得 \(push\_back\) \(angle[i]+360\)

最后利用滑动窗口即可

点击查看代码
class Solution {
private:
    long double pi = acos(-1.0);
    
    long double GetAngle(long double y, long double x){
        return (atan2(y,x)*180.0)/pi;
    }
    
    vector<long double> angles;
    int ans=0;
    
public:
    int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) {
        int sx = location[0], sy = location[1];
        int n = points.size();
        for(int i=0;i<n;i++){
            if(points[i][0]==sx && points[i][1]==sy)ans++;
            else{
                long double dx = 1.0*(points[i][0]-sx);
                long double dy = 1.0*(points[i][1]-sy);
                
                long double ang = GetAngle(dy, dx);
                angles.push_back(ang);
            }
        }
        sort(angles.begin(), angles.end());
        n = angles.size();
        for(int i=0;i<n;i++){
            // circle
            angles.push_back(angles[i]+360.0);
        }
        int l = 0, cnt = 0;
        for(int i=0;i<angles.size();i++){
            while(angles[i]-angles[l]>angle)l++;
            cnt=max(cnt, i-l+1);
        }
        ans+=cnt;
        return ans;
    }
};

标签:Google,angle,int,Number,极角,location,view,your,points
来源: https://www.cnblogs.com/xinyu04/p/16614798.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有