Shuffle an Array - Problem
๐ฒ Shuffle an Array - Random Permutation Generator
You are given an integer array nums and need to design an algorithm that can randomly shuffle the array while ensuring that all permutations are equally likely.
Challenge: Create a Solution class that maintains the original array and provides two key operations:
- Reset: Restore the array to its original configuration
- Shuffle: Generate a truly random permutation where each arrangement has equal probability
Key Requirements:
- Each of the
n!possible permutations should have probability1/n! - The shuffle should be unbiased - no arrangement should be favored over others
- Must be efficient for repeated shuffle operations
Example: For array [1,2,3], each of the 6 permutations [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] should appear with equal likelihood over many shuffles.
Input & Output
example_1.py โ Basic Usage
$
Input:
nums = [1, 2, 3]
solution = Solution(nums)
solution.shuffle()
solution.reset()
solution.shuffle()
โบ
Output:
[3, 1, 2] (example)
[1, 2, 3]
[1, 3, 2] (example)
๐ก Note:
The shuffle() method returns a random permutation. Each call produces a potentially different result. reset() always returns the original array [1,2,3].
example_2.py โ Single Element
$
Input:
nums = [1]
solution = Solution(nums)
solution.shuffle()
solution.reset()
โบ
Output:
[1]
[1]
๐ก Note:
With only one element, there's only one possible permutation. Both shuffle() and reset() return [1].
example_3.py โ Larger Array
$
Input:
nums = [1, 2, 3, 4, 5]
solution = Solution(nums)
solution.shuffle()
solution.reset()
โบ
Output:
[4, 1, 5, 2, 3] (example)
[1, 2, 3, 4, 5]
๐ก Note:
With 5 elements, there are 5! = 120 possible permutations. The Fisher-Yates algorithm ensures each has equal 1/120 probability.
Visualization
Tap to expand
Understanding the Visualization
1
Start from End
Begin with the last element in the array. This represents the bottom card in our deck.
2
Random Selection
Generate a random index from 0 to current position (inclusive). This chooses which card to swap with.
3
Swap Elements
Exchange the current element with the randomly selected element. This ensures randomness.
4
Move Backward
Move to the previous position and repeat. Each position gets exactly one chance to be swapped.
5
Perfect Shuffle
After processing all positions, every permutation has exactly 1/n! probability.
Key Takeaway
๐ฏ Key Insight: The Fisher-Yates shuffle achieves perfect randomness by making exactly n sequential random decisions, ensuring each of the n! permutations has equal probability 1/n! while using only O(n) time and O(1) extra space.
Time & Space Complexity
Time Complexity
O(n)
Single pass through array, each swap is O(1)
โ Linear Growth
Space Complexity
O(n)
Only need to store original array copy for reset functionality
โก Linearithmic Space
Constraints
- 1 โค nums.length โค 50
- -106 โค nums[i] โค 106
- At most 104 calls will be made to reset and shuffle combined
- All elements in nums are unique
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code