面试的第二题,
class Solution {
public:
Solution(vector<vector<int>>& rects) {
for(const auto& rect:rects){
for(int x=rect.at(0);x<=rect.at(2);++x){
for(int y=rect.at(1);y<=rect.at(3);++y){
m_legalPoints.push_back(std::make_pair(x,y));
}
}
}
}
vector<int> pick() {
int iRand = rand()%m_legalPoints.size();
std::vector<int> ret;
ret.push_back(m_legalPoints.at(iRand).first);
ret.push_back(m_legalPoints.at(iRand).second);
return ret;
}
std::vector<std::pair<int,int>> m_legalPoints;
};
/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(rects);
* vector<int> param_1 = obj->pick();
*/
上面这个超出时间限制了,换种方法,
思路:每个矩形所含点的数量不同,根据点数视为“一维线段”,相连之,前缀和,生成随机数得在哪个矩形取点,再随机取此矩形的店?