Skip to content

Commit f7ee30c

Browse files
committed
Time: 68 ms (98.86%), Space: 40.4 MB (60.23%) - LeetHub
1 parent 8b85dd3 commit f7ee30c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
vector<int> cum;
3+
public:
4+
5+
Solution(vector<int>& w) {
6+
int prev = 0;
7+
for(auto x: w){
8+
cum.push_back(x + prev);
9+
prev = cum.back();
10+
}
11+
}
12+
13+
int pickIndex() {
14+
int total = cum.back();
15+
int val = rand() % total + 1;
16+
return lower_bound(cum.begin(), cum.end(), val) - cum.begin();
17+
}
18+
};
19+
20+
/**
21+
* Your Solution object will be instantiated and called as such:
22+
* Solution* obj = new Solution(w);
23+
* int param_1 = obj->pickIndex();
24+
*/

0 commit comments

Comments
 (0)