/*
* @lc app=leetcode id=528 lang=cpp
*
* [528] Random Pick with Weight
*/
// @lc code=start
class Solution {
vector<int> elems;
public:
Solution(vector<int>& w) {
elems.resize(w.size());
for (int i = 0, c = 0; i < w.size(); ++i) {
c += w[i];
elems[i] = c;
}
}
int pickIndex() {
int i = rand() % elems.back();
return upper_bound(elems.begin(), elems.end(), i) - elems.begin();
}
};
/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(w);
* int param_1 = obj->pickIndex();
*/
// @lc code=end