生活随笔
收集整理的這篇文章主要介紹了
LeetCode 1610. 可见点的最大数目(atan2函数求夹角)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
1. 題目
給你一個點數組 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
= [[0,1],[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
<= 10^9
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/maximum-number-of-visible-points
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
2. 解題
http://www.cplusplus.com/reference/cmath/atan2/?kw=atan2
class Solution {
public:int visiblePoints(vector
<vector
<int>>& points
, int angle
, vector
<int>& location
) {int x
, y
, overlap
= 0;double PI
= 3.141592653, eps
= 1e-8;vector
<double> ang
;for(int i
= 0 ; i
< points
.size() ; i
++){x
= points
[i
][0]-location
[0];y
= points
[i
][1]-location
[1];if(x
== 0 && y
== 0){overlap
++;}else{ang
.push_back(atan2(y
, x
)*180/PI
);}}sort(ang
.begin(), ang
.end());int n
= ang
.size();for(int i
= 0; i
< n
; i
++)ang
.push_back(ang
[i
]+360);int j
= 0, ans
= 0;for(int i
= 0; i
< n
; i
++){while(j
< ang
.size() && ang
[j
]-ang
[i
] <= angle
+eps
){ans
= max(ans
, j
-i
+1);j
++;}}return ans
+overlap
;}
};
1148 ms 128.5 MB
我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的LeetCode 1610. 可见点的最大数目(atan2函数求夹角)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。