leetcode 困难题 1610. 可见点的最大数目-Maximum Number of Visible Points

张开发
2026/6/7 18:50:29 15 分钟阅读
leetcode 困难题 1610. 可见点的最大数目-Maximum Number of Visible Points
Problem: 1610. 可见点的最大数目-Maximum Number of Visible Points根据余弦定理求出所有点到x轴正方向的夹角全部换算成角度不使用弧度的而且逆时针从0度到360度根据象限求出0360度没有负数的排序然后使用二分查找的upper_boundALLangle[i] angle1e-5而且上界可能360此时需要返回从索引0开始查找if(ALLangle[i] angle1e-5 ALLangle[j] 360.0) ind;拿到最大值Codeclass Solution { public: int visiblePoints(vectorvectorint points, int angle, vectorint location) { int n points.size(), x0location[0], y0 location[1]; int ans 0, ind, mx INT_MIN; vectordouble ALLangle; double a0 1, a1 0, x, y, x1, y1, ang, now; for(int i 0; i n; i) { x points[i][0]; y points[i][1]; x1 x - x0; y1 y - y0; if(x10 y10) { ans; continue; } ang acos(abs(x1) / sqrt(x1*x1 y1*y1)) * 180.0 / (double)M_PI; if(x10 y10) {} else if(x1 0 y1 0) ang 180 - ang; else if(x1 0 y1 0) ang 180 ang; else if(x1 0 y1 0) ang 360 - ang; ALLangle.push_back(ang); } mx ans; sort(ALLangle.begin(), ALLangle.end()); int m ALLangle.size(); for(int i 0; i m; i) { now ALLangle[i] angle1e-5; ind upper_bound(ALLangle.begin(), ALLangle.end(), now) - ALLangle.begin(); if(now 360.0) { for(int j 0; j i; j) { if(now ALLangle[j] 360.0) ind; else break; } } mx max(ans ind - i, mx); } return mx; } };

更多文章