Practice/Uber/Leetcode 384. Shuffle an Array
CodingMust
Implement a data structure that maintains an integer array and provides two key operations:
The shuffle operation must be truly unbiased, meaning if there are n! possible permutations of the array, each one should have exactly 1/n! probability of occurring.
Example 1:
RandomArray ra = new RandomArray([1, 2, 3]) ra.shuffle() // Returns [3, 1, 2] (one of 6 possible permutations) ra.reset() // Returns [1, 2, 3] ra.shuffle() // Returns [2, 3, 1] (any permutation possible)
Example 2:
RandomArray ra = new RandomArray([4, 4, 5]) ra.shuffle() // Returns [4, 5, 4] or any permutation ra.reset() // Returns [4, 4, 5] ra.shuffle() // Returns [5, 4, 4] or any permutation ra.reset() // Returns [4, 4, 5]
Example 3:
RandomArray ra = new RandomArray([1]) ra.shuffle() // Returns [1] (only one permutation exists) ra.reset() // Returns [1]