"content":"<p>You are given an array of non-overlapping axis-aligned rectangles <code>rects</code> where <code>rects[i] = [a<sub>i</sub>, b<sub>i</sub>, x<sub>i</sub>, y<sub>i</sub>]</code> indicates that <code>(a<sub>i</sub>, b<sub>i</sub>)</code> is the bottom-left corner point of the <code>i<sup>th</sup></code> rectangle and <code>(x<sub>i</sub>, y<sub>i</sub>)</code> is the top-right corner point of the <code>i<sup>th</sup></code> rectangle. Design an algorithm to pick a random integer point inside the space covered by one of the given rectangles. A point on the perimeter of a rectangle is included in the space covered by the rectangle.</p>\n\n<p>Any integer point inside the space covered by one of the given rectangles should be equally likely to be returned.</p>\n\n<p><strong>Note</strong> that an integer point is a point that has integer coordinates.</p>\n\n<p>Implement the <code>Solution</code> class:</p>\n\n<ul>\n\t<li><code>Solution(int[][] rects)</code> Initializes the object with the given rectangles <code>rects</code>.</li>\n\t<li><code>int[] pick()</code> Returns a random integer point <code>[u, v]</code> inside the space covered by one of the given rectangles.</li>\n</ul>\n\n<p> </p>\n<p><strong>Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/24/lc-pickrandomrec.jpg\" style=\"width: 419px; height: 539px;\" />\n<pre>\n<strong>Input</strong>\n["Solution", "pick", "pick", "pick", "pick", "pick"]\n[[[[-2, -2, 1, 1], [2, 2, 4, 6]]], [], [], [], [], []]\n<strong>Output</strong>\n[null, [1, -2], [1, -1], [-1, -2], [-2, -2], [0, 0]]\n\n<strong>Explanation</strong>\nSolution solution = new Solution([[-2, -2, 1, 1], [2, 2, 4, 6]]);\nsolution.pick(); // return [1, -2]\nsolution.pick(); // return [1, -1]\nsolution.pick(); // return [-1, -2]\nsolution.pick(); // return [-2, -2]\nsolution.pick(); // return [0, 0]\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 <= rects.length <= 100</code></li>\n\t<li><code>rects[i].length == 4</code></li>\n\t<li><code>-10<sup>9</sup> <= a<sub>i</sub> < x<sub>i</sub> <= 10<sup>9</sup></code></li>\n\t<li><code>-10<sup>9</sup> <= b<sub>i</sub> < y<sub>i</sub> <= 10<sup>9</sup></code></li>\n\t<li><code>x<sub>i</sub> - a<sub>i</sub> <= 2000</code></li>\n\t<li><code>y<sub>i</sub> - b<sub>i</sub> <= 2000</code></li>\n\t<li>All the rectangles do not overlap.</li>\n\t<li>At most <code>10<sup>4</sup></code> calls will be made to <code>pick</code>.</li>\n</ul>\n",
"similarQuestions":"[{\"title\": \"Random Pick with Weight\", \"titleSlug\": \"random-pick-with-weight\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Generate Random Point in a Circle\", \"titleSlug\": \"generate-random-point-in-a-circle\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
"code":"class Solution {\npublic:\n Solution(vector<vector<int>>& rects) {\n \n }\n \n vector<int> pick() {\n \n }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(rects);\n * vector<int> param_1 = obj->pick();\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Java",
"langSlug":"java",
"code":"class Solution {\n\n public Solution(int[][] rects) {\n \n }\n \n public int[] pick() {\n \n }\n}\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution obj = new Solution(rects);\n * int[] param_1 = obj.pick();\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Python",
"langSlug":"python",
"code":"class Solution(object):\n\n def __init__(self, rects):\n \"\"\"\n :type rects: List[List[int]]\n \"\"\"\n \n\n def pick(self):\n \"\"\"\n :rtype: List[int]\n \"\"\"\n \n\n\n# Your Solution object will be instantiated and called as such:\n# obj = Solution(rects)\n# param_1 = obj.pick()",
"__typename":"CodeSnippetNode"
},
{
"lang":"Python3",
"langSlug":"python3",
"code":"class Solution:\n\n def __init__(self, rects: List[List[int]]):\n \n\n def pick(self) -> List[int]:\n \n\n\n# Your Solution object will be instantiated and called as such:\n# obj = Solution(rects)\n# param_1 = obj.pick()",
"__typename":"CodeSnippetNode"
},
{
"lang":"C",
"langSlug":"c",
"code":"\n\n\ntypedef struct {\n \n} Solution;\n\n\nSolution* solutionCreate(int** rects, int rectsSize, int* rectsColSize) {\n \n}\n\nint* solutionPick(Solution* obj, int* retSize) {\n \n}\n\nvoid solutionFree(Solution* obj) {\n \n}\n\n/**\n * Your Solution struct will be instantiated and called as such:\n * Solution* obj = solutionCreate(rects, rectsSize, rectsColSize);\n * int* param_1 = solutionPick(obj, retSize);\n \n * solutionFree(obj);\n*/",
"__typename":"CodeSnippetNode"
},
{
"lang":"C#",
"langSlug":"csharp",
"code":"public class Solution {\n\n public Solution(int[][] rects) {\n \n }\n \n public int[] Pick() {\n \n }\n}\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution obj = new Solution(rects);\n * int[] param_1 = obj.Pick();\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"JavaScript",
"langSlug":"javascript",
"code":"/**\n * @param {number[][]} rects\n */\nvar Solution = function(rects) {\n \n};\n\n/**\n * @return {number[]}\n */\nSolution.prototype.pick = function() {\n \n};\n\n/** \n * Your Solution object will be instantiated and called as such:\n * var obj = new Solution(rects)\n * var param_1 = obj.pick()\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Ruby",
"langSlug":"ruby",
"code":"class Solution\n\n=begin\n :type rects: Integer[][]\n=end\n def initialize(rects)\n \n end\n\n\n=begin\n :rtype: Integer[]\n=end\n def pick()\n \n end\n\n\nend\n\n# Your Solution object will be instantiated and called as such:\n# obj = Solution.new(rects)\n# param_1 = obj.pick()",
"__typename":"CodeSnippetNode"
},
{
"lang":"Swift",
"langSlug":"swift",
"code":"\nclass Solution {\n\n init(_ rects: [[Int]]) {\n \n }\n \n func pick() -> [Int] {\n \n }\n}\n\n/**\n * Your Solution object will be instantiated and called as such:\n * let obj = Solution(rects)\n * let ret_1: [Int] = obj.pick()\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Go",
"langSlug":"golang",
"code":"type Solution struct {\n \n}\n\n\nfunc Constructor(rects [][]int) Solution {\n \n}\n\n\nfunc (this *Solution) Pick() []int {\n \n}\n\n\n/**\n * Your Solution object will be instantiated and called as such:\n * obj := Constructor(rects);\n * param_1 := obj.Pick();\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Scala",
"langSlug":"scala",
"code":"class Solution(_rects: Array[Array[Int]]) {\n\n def pick(): Array[Int] = {\n \n }\n\n}\n\n/**\n * Your Solution object will be instantiated and called as such:\n * var obj = new Solution(rects)\n * var param_1 = obj.pick()\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Kotlin",
"langSlug":"kotlin",
"code":"class Solution(rects: Array<IntArray>) {\n\n fun pick(): IntArray {\n \n }\n\n}\n\n/**\n * Your Solution object will be instantiated and called as such:\n * var obj = Solution(rects)\n * var param_1 = obj.pick()\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Rust",
"langSlug":"rust",
"code":"struct Solution {\n\n}\n\n\n/** \n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl Solution {\n\n fn new(rects: Vec<Vec<i32>>) -> Self {\n \n }\n \n fn pick(&self) -> Vec<i32> {\n \n }\n}\n\n/**\n * Your Solution object will be instantiated and called as such:\n * let obj = Solution::new(rects);\n * let ret_1: Vec<i32> = obj.pick();\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"PHP",
"langSlug":"php",
"code":"class Solution {\n /**\n * @param Integer[][] $rects\n */\n function __construct($rects) {\n \n }\n \n /**\n * @return Integer[]\n */\n function pick() {\n \n }\n}\n\n/**\n * Your Solution object will be instantiated and called as such:\n * $obj = Solution($rects);\n * $ret_1 = $obj->pick();\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"TypeScript",
"langSlug":"typescript",
"code":"class Solution {\n constructor(rects: number[][]) {\n\n }\n\n pick(): number[] {\n\n }\n}\n\n/**\n * Your Solution object will be instantiated and called as such:\n * var obj = new Solution(rects)\n * var param_1 = obj.pick()\n */",
"__typename":"CodeSnippetNode"
},
{
"lang":"Racket",
"langSlug":"racket",
"code":"(define solution%\n (class object%\n (super-new)\n\n ; rects : (listof (listof exact-integer?))\n (init-field\n rects)\n \n ; pick : -> (listof exact-integer?)\n (define/public (pick)\n\n )))\n\n;; Your solution% object will be instantiated and called as such:\n;; (define obj (new solution% [rects rects]))\n;; (define param_1 (send obj pick))",
"__typename":"CodeSnippetNode"
},
{
"lang":"Erlang",
"langSlug":"erlang",
"code":"-spec solution_init_(Rects :: [[integer()]]) -> any().\nsolution_init_(Rects) ->\n .\n\n-spec solution_pick() -> [integer()].\nsolution_pick() ->\n .\n\n\n%% Your functions will be called as such:\n%% solution_init_(Rects),\n%% Param_1 = solution_pick(),\n\n%% solution_init_ will be called before every test case, in which you can do some necessary initializations.",
"__typename":"CodeSnippetNode"
},
{
"lang":"Elixir",
"langSlug":"elixir",
"code":"defmodule Solution do\n @spec init_(rects :: [[integer]]) :: any\n def init_(rects) do\n\n end\n\n @spec pick() :: [integer]\n def pick() do\n\n end\nend\n\n# Your functions will be called as such:\n# Solution.init_(rects)\n# param_1 = Solution.pick()\n\n# Solution.init_ will be called before every test case, in which you can do some necessary initializations.",
"envInfo":"{\"cpp\": [\"C++\", \"<p>Compiled with <code> clang 11 </code> using the latest C++ 17 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\"], \"java\": [\"Java\", \"<p><code> OpenJDK 17 </code>. Java 8 features such as lambda expressions and stream API can be used. </p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>\"], \"python\": [\"Python\", \"<p><code>Python 2.7.12</code>.</p>\\r\\n\\r\\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\\\"https://docs.python.org/2/library/array.html\\\" target=\\\"_blank\\\">array</a>, <a href=\\\"https://docs.python.org/2/library/bisect.html\\\" target=\\\"_blank\\\">bisect</a>, <a href=\\\"https://docs.python.org/2/library/collections.html\\\" target=\\\"_blank\\\">collections</a>. If you need more libraries, you can import it yourself.</p>\\r\\n\\r\\n<p>For Map/TreeMap data structure, you may use <a href=\\\"http://www.grantjenks.com/docs/sortedcontainers/\\\" target=\\\"_blank\\\">sortedcontainers</a> library.</p>\\r\\n\\r\\n<p>Note that Python 2.7 <a href=\\\"https://www.python.org/dev/peps/pep-0373/\\\" target=\\\"_blank\\\">will not be maintained past 2020</a>. For the latest Python, please choose Python3 instead.</p>\"], \"c\": [\"C\", \"<p>Compiled with <code>gcc 8.2</code> using the gnu99 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level one optimization (<code>-O1</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n\\r\\n<p>For hash table operations, you may use <a href=\\\"https://troydhanson.github.io/uthash/\\\" target=\\\"_blank\\\">uthash</a>. \\\"uthash.h\\\" is included by default. Below are some examples:</p>\\r\\n\\r\\n<p><b>1. Adding an item to a hash.</b>\\r\\n<pre>\\r\\nstruct hash_entry {\\r\\n int id; /* we'll use this field as the key */\\r\\n char name[10];\\r\\n UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>2. Looking up an item in a hash:</b>\\r\\n<pre>\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n return s;\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>3. Deleting an item in a hash:</b>\\r\\n<pre>\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n HASH_DEL(users, user); \\r\\n}\\r\\n</pre>\\r\\n</p>\"], \"csharp\": [\"C#\", \"<p><a href=\\\"https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9\\\" target=\\\"_blank\\\">C# 10 with .NET 6 runtime</a></p>\\r\\n\\r\\n<p>Your code is compiled with debug flag enabled (<code>/debug</code>).</p>\"], \"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use <a href=\\\"https://github.com/datastructures-js/priority-queue\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and <a href=\\\"https: