/* * @lc app=leetcode id=528 lang=cpp * * [528] Random Pick with Weight */// @lc code=startclassSolution { 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; } }intpickIndex() {int i =rand() %elems.back();returnupper_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