Advertisement

Shuffle an Array - LeetCode 384 Solution

Shuffle an Array - Complete Solution Guide

Shuffle an Array is LeetCode problem 384, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array nums , design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling. Implement the Solution class: Solution(int[] nums) Initializes the object with the integer array nums . int[] reset() Resets the array to its original configuration and returns it. int[] shuffle() Returns a random shuffling of the array. Example 1: Input ["Solution", "shuffle", "reset", "shuffle"] [[[1, 2, 3]], [], [], []] Output [n

Detailed Explanation

The problem requires designing a class that can shuffle an array of integers randomly, reset the array to its original state, and do both efficiently. The core requirement is that all permutations of the array must have an equal probability of being generated by the shuffle function. The input is an integer array `nums`. The class should have three methods: `Solution(nums)` to initialize the object, `reset()` to return the original array, and `shuffle()` to return a randomly shuffled array.

Solution Approach

The provided solution implements the Fisher-Yates shuffle algorithm within the `shuffle()` method and maintains a copy of the original array to implement the `reset()` method. The `Solution` class stores two copies of the input array: `_original` (the initial configuration) and `_array` (the current configuration, which gets shuffled). The `reset()` method restores `_array` from `_original`. The `shuffle()` method iterates through the array, swapping each element with a randomly chosen element from the remaining unsorted portion of the array.

Step-by-Step Algorithm

  1. Step 1: In the constructor, `Solution(nums)`, create a copy of the input array `nums` and store it in `_original`. Also, create another copy and store it in `_array`. This ensures the original array remains unchanged and `_array` can be shuffled and reset.
  2. Step 2: The `reset()` method simply copies the content of `_original` back into `_array` and returns `_array`.
  3. Step 3: The `shuffle()` method implements the Fisher-Yates shuffle. Iterate from the end of the array to the beginning.
  4. Step 4: For each index `i`, generate a random index `j` between `0` and `i` (inclusive).
  5. Step 5: Swap the elements at indices `i` and `j` in `_array`.
  6. Step 6: After iterating through the entire array, return the shuffled `_array`.

Key Insights

  • Insight 1: The Fisher-Yates shuffle algorithm is the standard algorithm for generating a random permutation of a finite set in linear time.
  • Insight 2: Preserving the original array is crucial to implementing the `reset()` function correctly. This requires storing a copy of the initial array.
  • Insight 3: Using a separate `_array` to store the current state enables efficient shuffling and resetting without modifying the original input array directly.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Math, Design, Randomized.

Companies

Asked at: J.P. Morgan, LinkedIn, Nvidia.