Random Pick with Weight
LeetCode Problem #528 (Medium)
Problem Statement
Given an array w
of positive integers, where w[i]
describes the weight of index i
, write a function pickIndex
which randomly picks an index in proportion to its weight.
For example, given an input list of values [1, 9], when we pick up a number out of it, the chance is that 9 times out of 10 we should pick the number 9 as the answer.
Testcases
Input Syntax Explanation
The input is two lists: the subroutines called and their arguments. Solution
's constructor has one argument, the array w
. pickIndex
has no arguments. Arguments are always wrapped with a list, even if there aren't any.
Prerequisite Knowledge
Modulus Operation
a % b when a > b
If a = 16 and b = 15,
16 % 15 = 1 since 15 * 1 + 1 = 16
a % b when a < b
If a = 6 and b = 15,
6 % 15 = 6 since 15 * 0 + 6 = 6
Binary Search
Binary search is an efficient searching algorithm which is performed on a sorted array by repeatedly dividing the search interval in half, where the original size of the search interval is the size of the array.
The target value is compared with the mid-value of the seach interval during each iteration, and the decision of whether to continue searching in the left half or right half is taken based on whether the target value is greater than the mid-value or less than the mid-value.
Since the array is already sorted, if the target value is less than the mid value, we know that the value can only be found in the left half of the array, which contains elements which are less than the mid-value. Similarly, if the target value is greater than the mid-value, it is only reasonable to assume that it will be located in the right half of the array, where the elements are greater than the mid-value.
Computing Middle Index
To find the index of the middle element during each iteration, we must find the rounded average of the left (minimum) and right (maximum) pointer values.
Conventionally, we would compute this as:
However, if we take overflow into consideration, then this computation would not work. If, for example, the maximum value that an integer can have is 100, and our left and right values are 40 and 80.
Using the intuitive formula, when we add left and right, we get:
120 is clearly greater than 100, which is the maximum range of the particular integer. This is an overflow.
Now, if we apply the formula:
Since right > left, when we subtract the value of left from right, the result will always be a smaller number, thus effectively preventing overflow.
Now when we compute the mid-value, we get:
Since 40 is within the bounds of the integer range, no overflow occurs.
Problem Explanation
Example:
Consider an array of weights: [3, 7, 1, 4].
Each value in the array corresponds to the frequency of occurrence of the element at that index value.
Element 0 occurs 3 times.
Element 1 occurs 7 times.
Element 2 occurs 1 time.
Element 3 occurs 4 times.
The total sum of weights is 3 + 7 + 1 + 4 = 15.
We can calculate the probability of each element occurring:
We need to generate the probability mass function of each index.
Suppose we have an array of 15 equal elements.
To get an index value and the corresponding mapping to the respective element, we generate a random number and take its modulo to 15.
Assume the random number is 6.
Therefore, it is in the range [3-9] so it is mapped back to Element 1.
Algorithm
Binary Search Approach
Store the lower and upper range of each weight according to its occurrence.
Generate a random index value and perform modulus with the total number of elements.
Search through the vector storing ranges using binary search to find the index value.
Code
Last updated
Was this helpful?