Javascript O(n) initiation O(1) picking using Alias Method
\nvar Solution = function(rects) {\n  // Imagine we put the boxes into a 2D pool and pick one. Then the bigger box will more likely to be seen and picked\n  // 
```\nvar Solution = function(rects) {\n  // Imagine we put the boxes into a 2D pool and pick one. Then the bigger box will more likely to be seen and picked\n  // So first we initialize the pool with the area of the boxes\n  let totalArea = 0;\n  this.boxes = rects.map(([x1, y1, x2, y2], index, area) => (\n    totalArea += area = (x2 - x1 + 1) * (y2 - y1 + 1),\n    { area, index, x1, y1, x2, y2 }\n  ));\n  // Alias method says that, for example you have n boxes, and n rooms with can contain exactly the average area of the boxes,\n  // you can distribute the content of the boxes into the rooms such that each room contains only content of 2 type of boxes.\n  // So the idea is putting each box into each room. Then some box will be larger than the room, and some will be smaller\n  // You can then split the larger box and put into the room which contains the smaller box\n  // So now let\'s classify the boxes base on if it\'s larger or smaller than the room size\n  const avgArea = this.avg = totalArea / rects.length;\n  const smallers = this.boxes.filter(box => box.area < avgArea);\n  const largers = this.boxes.filter(box => box.area > avgArea);\n  while (smallers.length !== 0 && largers.length !== 0) {\n    const smaller = smallers.shift();\n    const larger = largers[0];\n    smaller.added = larger.index;\n    larger.area -= avgArea - smaller.area;\n\t// After splitting the larger box, calling A for example, and put into the room of the smaller box, that room is now full, and we no longer care about it\n\t// About the box A, if it is now still bigger than the room capacity, keep it in the "largers" stack to work with later\n\t// If it is now fit into its room, forget about it\n\t// If it becomes smaller for a typical room, we put it into the "smallers" stack to find it a new "larger boxie"\n    larger.area <= avgArea && (\n      largers.shift(),\n      larger.area < avgArea && (smallers[smallers.length] = larger)\n    );\n  }\n  this.pick = function() {\n    // Now we just need to pick a random room\n    const box = this.boxes[Math.random() * this.boxes.length | 0];\n\t// And then pick 1 of the 2 boxes in that room, based on their area as above\n    const { x1, y1, x2, y2 } = Math.random() * this.avg > box.area ? this.boxes[box.added] : box;\n\t// Finally return a point inside that box\n    return [\n      Math.floor(x1 + Math.random() * (x2 - x1 + 1)),\n      Math.floor(y1 + Math.random() * (y2 - y1 + 1))\n    ];\n  }\n};\n```\n![Alias method](https://www.researchgate.net/profile/Mattia_Massone/publication/326356402/figure/fig10/AS:647622192799747@1531416571879/Example-of-alias-method.png)
['JavaScript']