Math Fundamentals18 sections · 814 units
Open in Course

Problem - Weighted Random

(LeetCode 528)

Given an array of weights, implement a function that randomly picks an index proportional to its weight.

If weights = [1, 3, 2], index 1 should be picked 3 times as often as index 0. This uses probability and prefix sums.

Read the full problem