1class Solution {2constructor(nums) {3this.map = new Map();4this.sum = 0;56nums.forEach((num, i) => {7this.sum += num;8this.map.set(this.sum, i);9});10}11pickIndex = function () {12const random_sum = Math.floor(Math.random() * this.sum);13return this.fetchIndexUsingBS(random_sum);14};15fetchIndexUsingBS = function (target) {16const sums = Array.from(this.map.keys());17let lo = 0,18hi = sums.length - 1,19mid;2021while (lo <= hi) {22mid = 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// }26if (sums[mid] > target) {27hi = mid - 1;28} else {29lo = mid + 1;30}31}32return lo;33};34}