{"id":3342,"date":"2018-07-28T17:45:20","date_gmt":"2018-07-29T00:45:20","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3342"},"modified":"2018-07-28T17:52:58","modified_gmt":"2018-07-29T00:52:58","slug":"leetcode-882-random-point-in-non-overlapping-rectangles","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-882-random-point-in-non-overlapping-rectangles\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 882. Random Point in Non-overlapping Rectangles"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a list of\u00a0<strong>non-overlapping<\/strong>\u00a0axis-aligned rectangles\u00a0<code>rects<\/code>, write a function\u00a0<code>pick<\/code>\u00a0which randomly and uniformily picks an\u00a0<strong>integer point<\/strong>\u00a0in the space\u00a0covered by the rectangles.<\/p>\n<p>Note:<\/p>\n<ol>\n<li>An\u00a0<strong>integer point<\/strong>\u00a0is a point that has integer coordinates.<\/li>\n<li>A point\u00a0on the perimeter\u00a0of a rectangle is\u00a0<strong>included<\/strong>\u00a0in the space covered by the rectangles.<\/li>\n<li><code>i<\/code>th rectangle =\u00a0<code>rects[i]<\/code>\u00a0=\u00a0<code>[x1,y1,x2,y2]<\/code>, where\u00a0<code>[x1, y1]<\/code>\u00a0are the integer coordinates of the bottom-left corner, and\u00a0<code>[x2, y2]<\/code>\u00a0are the integer coordinates of the top-right corner.<\/li>\n<li>length and width of each rectangle does not exceed\u00a0<code>2000<\/code>.<\/li>\n<li><code>1 &lt;= rects.length\u00a0&lt;= 100<\/code><\/li>\n<li><code>pick<\/code>\u00a0return a point as an array of integer coordinates\u00a0<code>[p_x, p_y]<\/code><\/li>\n<li><code>pick<\/code>\u00a0is called at most\u00a0<code>10000<\/code>\u00a0times.<\/li>\n<\/ol>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: \r\n<\/strong><span id=\"example-input-1-1\">[\"Solution\",\"pick\",\"pick\",\"pick\"]\r\n<\/span><span id=\"example-input-1-2\">[[[[1,1,5,5]]],[],[],[]]<\/span>\r\n<strong>Output: \r\n<\/strong><span id=\"example-output-1\">[null,[4,1],[4,1],[3,3]]<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: \r\n<\/strong><span id=\"example-input-2-1\">[\"Solution\",\"pick\",\"pick\",\"pick\",\"pick\",\"pick\"]\r\n<\/span><span id=\"example-input-2-2\">[[[[-2,-2,-1,-1],[1,0,3,0]]],[],[],[],[],[]]<\/span>\r\n<strong>Output: \r\n<\/strong><span id=\"example-output-2\">[null,[-1,-2],[2,0],[-2,-1],[3,0],[-2,-2]]<\/span><\/pre>\n<\/div>\n<div>\n<p><strong>Explanation of Input Syntax:<\/strong><\/p>\n<p>The input is two lists:\u00a0the subroutines called\u00a0and their\u00a0arguments.\u00a0<code>Solution<\/code>&#8216;s\u00a0constructor has one argument, the array of rectangles\u00a0<code>rects<\/code>.\u00a0<code>pick<\/code>\u00a0has no arguments.\u00a0Arguments\u00a0are\u00a0always wrapped with a list, even if there aren&#8217;t any.<\/p>\n<\/div>\n<h1><strong>Solution: Binary Search<\/strong><\/h1>\n<p>Same as\u00a0<a href=\"http:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-880-random-pick-with-weight\/\">LeetCode 880. Random Pick with Weight<\/a><\/p>\n<p>Use area of the rectangles as weights.<\/p>\n<p>Time complexity: Init: O(n) Pick: O(logn)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 92 ms\r\nclass Solution {\r\npublic:\r\n  Solution(vector&lt;vector&lt;int&gt;&gt; rects): rects_(std::move(rects)) {\r\n    sums_ = vector&lt;int&gt;(rects_.size());\r\n    for (int i = 0; i &lt; rects_.size(); ++i) {\r\n      sums_[i] = (rects_[i][2] - rects_[i][0] + 1) * (rects_[i][3] - rects_[i][1] + 1);\r\n      if (i &gt; 0) sums_[i] += sums_[i - 1];\r\n    }\r\n  }\r\n\r\n  vector&lt;int&gt; pick() {\r\n    const int s = nextInt(sums_.back()) + 1;\r\n    int index = lower_bound(sums_.begin(), sums_.end(), s) - sums_.begin();\r\n    const auto&amp; rect = rects_[index];\r\n    return {rect[0] + nextInt(rect[2] - rect[0] + 1),\r\n            rect[1] + nextInt(rect[3] - rect[1] + 1)};\r\n  }\r\n    \r\nprivate:  \r\n  vector&lt;int&gt; sums_;\r\n  vector&lt;vector&lt;int&gt;&gt; rects_;\r\n  \r\n  \/\/ Returns a random int in [0, n - 1]\r\n  int nextInt(int n) {\r\n    return rand() \/ static_cast&lt;double&gt;(RAND_MAX) * n;\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-880-random-pick-with-weight\/\">\u82b1\u82b1\u9171 LeetCode 880. Random Pick with Weight<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a list of\u00a0non-overlapping\u00a0axis-aligned rectangles\u00a0rects, write a function\u00a0pick\u00a0which randomly and uniformily picks an\u00a0integer point\u00a0in the space\u00a0covered by the rectangles. Note: An\u00a0integer point\u00a0is a point&#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":[284,31,177,257,91,357],"class_list":["post-3342","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-geometry","tag-math","tag-medium","tag-pick","tag-random","tag-weighted","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3342","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=3342"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3342\/revisions"}],"predecessor-version":[{"id":3350,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3342\/revisions\/3350"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}