Algorithm · Meta · Medium
Requirements The constructor receives w: list[int], where every weight is positive. pickIndex() -> int must choose index i with probability w[i] / sum(w). MLE follow-up: “what if this runs millions of times—how would you improve it?” Examples w = [2, 6] → P(0) = 1/4, P(1) = 3/4. This follows because the total weight is 8, so the two weights account for 2/8 and 6/8 of the outcomes. w = [5, 5, 5, 5] → each of the 4 indices is selected uniformly. Since all weights match, every…
Checking your access…