zoukankan      html  css  js  c++  java
  • leetcode1610 可见点的最大数目

    思路:

    将点按照极角排序后,用滑动窗口法求解。

    实现:

     1 class Solution
     2 {
     3 public:
     4     const double PI = acos(-1);
     5     int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location)
     6     {
     7         int cnt = 0;
     8         vector<double> v;
     9         for (auto& it: points)
    10         {
    11             if (it[0] == location[0] && it[1] == location[1])
    12             {
    13                 cnt++; continue;
    14             }
    15             double r = atan2(it[1] - location[1], it[0] - location[0]);
    16             if (r < 0) r += 2 * PI;
    17             r = r / PI * 180;
    18             v.push_back(r);
    19         }
    20         sort(v.begin(), v.end());
    21         int n = v.size();
    22         for (int i = 0; i < n; i++) v.push_back(v[i] + 360);
    23         int l = 0, r = 0, res = 0;
    24         while (r < 2 * n)
    25         {
    26             while (r < 2 * n && v[r] - v[l] <= angle) r++;
    27             res = max(res, r - l);
    28             while (r < 2 * n && l <= r && v[r] - v[l] > angle) l++;
    29             r++;
    30         }
    31         return res + cnt;
    32     }
    33 }
  • 相关阅读:
    windows通过Composer安装yii2
    jquery自定义函数
    js 回调
    读取.properties配置文件
    spring @ModelAttribute 注解
    excel导出
    spring定时器
    maven添加自己的jar包到本地仓库
    activeMq 消费者整合spring
    linux操作命令
  • 原文地址:https://www.cnblogs.com/wangyiming/p/13778612.html
Copyright © 2011-2022 走看看