{"id":8357,"date":"2021-04-17T11:57:55","date_gmt":"2021-04-17T18:57:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8357"},"modified":"2021-04-17T12:05:38","modified_gmt":"2021-04-17T19:05:38","slug":"leetcode-1828-queries-on-number-of-points-inside-a-circle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-1828-queries-on-number-of-points-inside-a-circle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1828. Queries on Number of Points Inside a Circle"},"content":{"rendered":"\n<p>You are given an array&nbsp;<code>points<\/code>&nbsp;where&nbsp;<code>points[i] = [x<sub>i<\/sub>, y<sub>i<\/sub>]<\/code>&nbsp;is the coordinates of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;point on a 2D plane. Multiple points can have the&nbsp;<strong>same<\/strong>&nbsp;coordinates.<\/p>\n\n\n\n<p>You are also given an array&nbsp;<code>queries<\/code>&nbsp;where&nbsp;<code>queries[j] = [x<sub>j<\/sub>, y<sub>j<\/sub>, r<sub>j<\/sub>]<\/code>&nbsp;describes a circle centered at&nbsp;<code>(x<sub>j<\/sub>, y<sub>j<\/sub>)<\/code>&nbsp;with a radius of&nbsp;<code>r<sub>j<\/sub><\/code>.<\/p>\n\n\n\n<p>For each query&nbsp;<code>queries[j]<\/code>, compute the number of points&nbsp;<strong>inside<\/strong>&nbsp;the&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;circle. Points&nbsp;<strong>on the border<\/strong>&nbsp;of the circle are considered&nbsp;<strong>inside<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<em>an array&nbsp;<\/em><code>answer<\/code><em>, where&nbsp;<\/em><code>answer[j]<\/code><em>&nbsp;is the answer to the&nbsp;<\/em><code>j<sup>th<\/sup><\/code><em>&nbsp;query<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><img decoding=\"async\" alt=\"\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/25\/chrome_2021-03-25_22-34-16.png\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> points = [[1,3],[3,3],[5,3],[2,2]], queries = [[2,3,1],[4,3,1],[1,1,2]]\n<strong>Output:<\/strong> [3,2,2]\n<strong>Explanation: <\/strong>The points and circles are shown above.\nqueries[0] is the green circle, queries[1] is the red circle, and queries[2] is the blue circle.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><img decoding=\"async\" alt=\"\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/25\/chrome_2021-03-25_22-42-07.png\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> points = [[1,1],[2,2],[3,3],[4,4],[5,5]], queries = [[1,2,2],[2,2,2],[4,3,2],[4,3,3]]\n<strong>Output:<\/strong> [2,3,2,4]\n<strong>Explanation: <\/strong>The points and circles are shown above.\nqueries[0] is green, queries[1] is red, queries[2] is blue, and queries[3] is purple.\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;= 500<\/code><\/li><li><code>points[i].length == 2<\/code><\/li><li><code>0 &lt;= x<sub>\u200b\u200b\u200b\u200b\u200b\u200bi<\/sub>, y<sub>\u200b\u200b\u200b\u200b\u200b\u200bi<\/sub>&nbsp;&lt;= 500<\/code><\/li><li><code>1 &lt;= queries.length &lt;= 500<\/code><\/li><li><code>queries[j].length == 3<\/code><\/li><li><code>0 &lt;= x<sub>j<\/sub>, y<sub>j<\/sub>&nbsp;&lt;= 500<\/code><\/li><li><code>1 &lt;= r<sub>j<\/sub>&nbsp;&lt;= 500<\/code><\/li><li>All coordinates are integers.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(P * Q)<br>Space complexity: O(1)<\/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  vector<int> countPoints(vector<vector<int>>& points, vector<vector<int>>& queries) {\n    vector<int> ans;\n    ans.reserve(queries.size());\n    for (const auto& q : queries) {\n      const int rs = q[2] * q[2];\n      int cnt = 0;      \n      for (const auto& p : points)\n        if ((q[0] - p[0]) * (q[0] - p[0]) + \n            (q[1] - p[1]) * (q[1] - p[1]) <= rs)\n          ++cnt;\n      ans.push_back(cnt);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array&nbsp;points&nbsp;where&nbsp;points[i] = [xi, yi]&nbsp;is the coordinates of the&nbsp;ith&nbsp;point on a 2D plane. Multiple points can have the&nbsp;same&nbsp;coordinates. You are also given&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127],"tags":[575,284,177],"class_list":["post-8357","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-circle","tag-geometry","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8357","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=8357"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8357\/revisions"}],"predecessor-version":[{"id":8359,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8357\/revisions\/8359"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}