Permutations

Permutations

·

3 min read

Explain the Problem

Given an array nums of distinct integers, return all possible permutations. You can return the answer in any order.

Example:

Input: nums = [1, 2, 3]

Output:

[
  [1, 2, 3],
  [1, 3, 2],
  [2, 1, 3],
  [2, 3, 1],
  [3, 1, 2],
  [3, 2, 1]
]

Short, Easy-to-Remember Solution/Approach

To solve this problem, we can use a recursive backtracking approach:

  1. Recursive Function: We'll create a helper function that builds permutations by swapping elements.

  2. Backtracking: If we reach the end of the array, we add the current permutation to the result list.

Solution in JavaScript with Code Commenting

Here's the JavaScript solution with detailed comments:

function permute(nums) {
  const result = [];

  // Helper function for backtracking
  function backtrack(start) {
    // If we reach the end of the array, add the current permutation to the result
    if (start === nums.length) {
      result.push([...nums]);
      return;
    }

    // Loop through the elements starting from the current index
    for (let i = start; i < nums.length; i++) {
      // Swap the current element with the start element
      [nums[start], nums[i]] = [nums[i], nums[start]];
      // Recurse with the next index
      backtrack(start + 1);
      // Backtrack: swap back the elements
      [nums[start], nums[i]] = [nums[i], nums[start]];
    }
  }

  // Start the backtracking with the first index
  backtrack(0);

  return result;
}

Explanation of the Solution in Simple Terms

Imagine you have a row of toys, and you want to find all the different ways you can arrange them.

  • Recursive Function: This is like a helper that tries different ways to arrange the toys by swapping their positions.

  • Backtracking: This means if the current arrangement doesn't need more swaps (either because you've considered all toys or you've tried all possibilities), you go back and try a different arrangement.

For each toy, you swap it with every other toy, then continue swapping the next toy in the row. If you reach the end of the row, you save the current arrangement. If not, you undo your last swap and try something else.

Code Explanation in Pointers

  1. Initialize Result Array: Create an empty array to store all permutations.

  2. Define Backtracking Function: Create a recursive function to explore different permutations.

  3. Base Case - End Reached: If the index reaches the end of the array, add the current permutation to the result.

  4. Loop Through Elements: Try swapping each element starting from the current index.

  5. Swap Elements: Swap the current element with the element at the current index.

  6. Recursive Call: Call the function recursively with the next index.

  7. Backtrack: Swap back the elements before trying the next one.

Complexity Analysis

  • Time Complexity: O(N * N!), where N is the number of elements in the input array. This is because we generate all permutations.

  • Space Complexity: O(N!), due to the recursion stack and the result list storing all permutations.