Reservoir Sampling (offline)
Given an array A
of n
elements and a number 0<=k<=n
, return a subset of A
of size k
.
Example
Solution
Idea:
Choose samples one by one
For each sample, choose a random integer which indicates the location, then swap this element to the beginning of the array
Time complexity:
Space complexity:
Last updated