Press "Enter" to skip to content

Posts tagged as “randomization”

花花酱 LeetCode 398. Random Pick Index

Problem:

https://leetcode.com/problems/random-pick-index/description/

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.

Note:
The array size can be very large. Solution that uses too much extra space will not pass the judge.

Example:

Solution: Reservoir sampling

https://en.wikipedia.org/wiki/Reservoir_sampling

Time complexity: O(query * n)

Space complexity: O(1)

C++

 

花花酱 LeetCode 384. Shuffle an Array

Shuffle a set of numbers without duplicates.

Example:

C++