Swift Explanation
A First Approach\nAn initial idea may be to select a rectangle at random then select a random point in that rectangle. However, we want to select a point unifor
**A First Approach**\nAn initial idea may be to select a rectangle at random then select a random point in that rectangle. However, we want to select a point uniformly at random from the entire space. Selecting a rectangle first, then selecting a point from that rectangle would give rectangles with small areas the same probability as rectangles with larger areas. Instead we need to find a method that gives rectangles with larger areas a higher probability. \n\n**A Correct Approach: Initialization**\nInitialization\n* We will store the total `area` of all the rectangles \n* To handle rectangles with `0` width or height we will add `1` to each dimension \n* We map the cumulative sum of the rectangles to the corresponding rectangle \n\nFor example, suppose we have rectangles `A, B, C` with area `2, 4, 8`. Then we would have `area = 14` and we would have \n``` \nareas = [2, 6, 14]\nmap = [\n 2: A, \n 6: B, \n 14: \n]\n```\n\n**A Correct Approach: Picking a Rectangle**\n1. Select any point in `0 ... area` and call it `rand`\n2. Select the first index in `areas` larger than or equal to `rand` \n3. Select the corresponding rectangle using `map[areas[idx]]`\n4. Select a random point from that rectangle\n\n**Solution**\n```swift\nclass Solution {\n    \n    // MARK: Properties \n    \n    var area = 0 \n    var areas = [Int]()\n    var map = [Int: [Int]]() \n    \n\n    // MARK: Initializer \n    \n    init(_ rects: [[Int]]) {\n        for idx in 0 ..< rects.count {\n            let rect = rects[idx]\n            area += (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1)\n            areas.append(area)\n            map[area] = rect\n        }\n    }\n    \n    func pick() -> [Int] {\n        let rand = Int.random(in: 0 ... area)\n        \n        for idx in 0 ..< areas.count {\n            if rand <= areas[idx] {\n                let rect = map[areas[idx]]!\n                return [\n                    Int.random(in: rect[0] ... rect[2]),\n                    Int.random(in: rect[1] ... rect[3])\n                ]\n            }\n        }\n        \n        return [] \n    }\n}\n```
[]