[C++] Random Points | Simple
Build a weighted list of rectangles where the weight is equal to each rectangle\'s area. When picking a random point, first pick a rect from the weighted list a
Build a weighted list of rectangles where the weight is equal to each rectangle\'s area. When picking a random point, first pick a rect from the weighted list and then pick a random point from within that rect.\n```\nclass Solution {\npublic:\n    vector<pair<int, vector<int>>> weightedRects;\n    long long totalWeight = 0;\n    \n    Solution(vector<vector<int>>& rects) {\n        for (auto& rect : rects) {\n            int area = (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1);\n            totalWeight += area;\n            weightedRects.push_back(make_pair(area, rect));\n        }\n    }\n    \n    vector<int> pick() {\n        int weight = rand() % (totalWeight + 1);\n        for (auto& entry : weightedRects) {\n            if (weight <= entry.first) {\n                auto& rect = entry.second;\n                int px = rect[0] + rand() % (rect[2] - rect[0] + 1);\n                int py = rect[1] + rand() % (rect[3] - rect[1] + 1);\n                return {px, py};\n            }\n            weight -= entry.first;\n        }\n        return {};\n    }\n};\n```
[]