{"id":7464,"date":"2020-10-04T12:26:47","date_gmt":"2020-10-04T19:26:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7464"},"modified":"2020-10-04T14:38:38","modified_gmt":"2020-10-04T21:38:38","slug":"leetcode-1610-maximum-number-of-visible-points","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1610-maximum-number-of-visible-points\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1610. Maximum Number of Visible Points"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1610. Maximum Number of Visible Points - \u5237\u9898\u627e\u5de5\u4f5c EP360\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/xn_VLwQnzvc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given an array&nbsp;<code>points<\/code>, an integer&nbsp;<code>angle<\/code>, and your&nbsp;<code>location<\/code>, where&nbsp;<code>location = [pos<sub>x<\/sub>, pos<sub>y<\/sub>]<\/code>&nbsp;and&nbsp;<code>points[i] = [x<sub>i<\/sub>, y<sub>i<\/sub>]<\/code>&nbsp;both denote&nbsp;<strong>integral coordinates<\/strong>&nbsp;on the X-Y plane.<\/p>\n\n\n\n<p>Initially, you are facing directly east from your position. You&nbsp;<strong>cannot move<\/strong>&nbsp;from your position, but you can&nbsp;<strong>rotate<\/strong>. In other words,&nbsp;<code>pos<sub>x<\/sub><\/code>&nbsp;and&nbsp;<code>pos<sub>y<\/sub><\/code>&nbsp;cannot be changed. Your field of view in&nbsp;<strong>degrees<\/strong>&nbsp;is represented by&nbsp;<code>angle<\/code>, determining&nbsp;how wide you can see from any given view direction. Let&nbsp;<code>d<\/code>&nbsp;be the amount in degrees that you rotate counterclockwise. Then, your field of view is the&nbsp;<strong>inclusive<\/strong>&nbsp;range of angles&nbsp;<code>[d - angle\/2, d + angle\/2]<\/code>.<\/p>\n\n\n\n<p>You can&nbsp;<strong>see<\/strong>&nbsp;some set of points if, for each point, the&nbsp;<strong>angle<\/strong>&nbsp;formed by the point, your position, and the immediate east direction from your position is&nbsp;<strong>in your field of view<\/strong>.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Return&nbsp;<em>the maximum number of points you can see<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/30\/89a07e9b-00ab-4967-976a-c723b2aa8656.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The shaded region represents your field of view. All points can be made visible in your field of view, including [3,3] even though [2,2] is in front and in the same line of sight.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> All points can be made visible in your field of view, including the one at your location.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/30\/5010bfd3-86e6-465f-ac64-e9df941d2e49.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> points = [[1,0],[2,1]], angle = 13, location = [1,1]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> You can only see one of the two points, as shown above.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= points.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>points[i].length == 2<\/code><\/li><li><code>location.length == 2<\/code><\/li><li><code>0 &lt;= angle &lt; 360<\/code><\/li><li><code>0 &lt;= pos<sub>x<\/sub>, pos<sub>y<\/sub>, x<sub>i<\/sub>, y<sub>i<\/sub>&nbsp;&lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding window<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1610-ep360.png\" alt=\"\" class=\"wp-image-7471\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1610-ep360.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1610-ep360-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1610-ep360-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Sort all the points by angle, duplicate the points with angle + 2*PI to deal with turn around case.<\/p>\n\n\n\n<p>maintain a window [l, r] such that angle[r] &#8211; angle[l] &lt;= fov<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& o) {    \n    int at_origin = 0;\n    vector<double> ps;\n    for (const auto& p : points)\n      if (p[0] == o[0] && p[1] == o[1])\n        ++at_origin;\n      else\n        ps.push_back(atan2(p[1] - o[1], p[0] - o[0]));\n    sort(begin(ps), end(ps));\n    const int n = ps.size();\n    for (int i = 0; i < n; ++i)\n      ps.push_back(ps[i] + 2.0 * M_PI); \/\/ duplicate the array +2PI\n    int l = 0;\n    int ans = 0;\n    double fov = angle * M_PI \/ 180.0;\n    for (int r = 0; r < ps.size(); ++r) {\n      while (ps[r] - ps[l] > fov) ++l;\n      ans = max(ans, r - l + 1);\n    }\n    return ans + at_origin;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array&nbsp;points, an integer&nbsp;angle, and your&nbsp;location, where&nbsp;location = [posx, posy]&nbsp;and&nbsp;points[i] = [xi, yi]&nbsp;both denote&nbsp;integral coordinates&nbsp;on the X-Y plane. Initially, you are facing&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[353,217,215],"class_list":["post-7464","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-gemoetry","tag-hard","tag-sliding-window","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7464"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7464\/revisions"}],"predecessor-version":[{"id":7472,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7464\/revisions\/7472"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}