1
class Solution {
2
constructor(nums) {
3
this.map = new Map();
4
this.sum = 0;
5

6
nums.forEach((num, i) => {
7
this.sum += num;
8
this.map.set(this.sum, i);
9
});
10
}
11
pickIndex = function () {
12
const random_sum = Math.floor(Math.random() * this.sum);
13
return this.fetchIndexUsingBS(random_sum);
14
};
15
fetchIndexUsingBS = function (target) {
16
const sums = Array.from(this.map.keys());
17
let lo = 0,
18
hi = sums.length - 1,
19
mid;
20

21
while (lo <= hi) {
22
mid = Math.floor((hi - lo) / 2) + lo;
23
// if((mid === 0 || sums[mid - 1] < target) && sums[mid] >= target) {
24
// return this.map.get(sums[mid]);
25
// }
26
if (sums[mid] > target) {
27
hi = mid - 1;
28
} else {
29
lo = mid + 1;
30
}
31
}
32
return lo;
33
};
34
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0