Advertisement

Permutations - LeetCode 46 Solution

Permutations - Complete Solution Guide

Permutations is LeetCode problem 46, 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 array nums of distinct integers, return all the possible permutations . You can return the answer in any order . Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2: Input: nums = [0,1] Output: [[0,1],[1,0]] Example 3: Input: nums = [1] Output: [[1]] Constraints: 1 <= nums.length <= 6 -10 <= nums[i] <= 10 All the integers of nums are unique .

Detailed Explanation

The problem requires generating all possible orderings (permutations) of a given array of distinct integers. The input is an array `nums` of integers, and the output should be a list of lists, where each inner list represents a unique permutation of the input array. The order of the permutations in the output doesn't matter. The key constraint is that all integers in the input array are distinct, simplifying the permutation generation process.

Solution Approach

The provided solutions use a backtracking algorithm to generate all permutations. The core idea is to recursively explore possible arrangements by swapping elements in the input array. The `backtrack` function iterates through the array, swapping the element at the current index with all subsequent elements. After each swap, it recursively calls itself to explore the remaining part of the array. After the recursive call returns, the swap is undone to explore other possibilities. When the recursion reaches the end of the array, it means a complete permutation has been formed, and this permutation is added to the result.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty list `result` to store the permutations.
  2. Step 2: Define a recursive function `backtrack(start_index)` to generate permutations starting from the given `start_index`.
  3. Step 3: Base Case: If `start_index` is equal to the length of the input array `n`, it means a complete permutation is formed. Add a copy of the current `nums` array to the `result` list and return.
  4. Step 4: Iterate through the elements from `start_index` to `n-1` using a `for` loop.
  5. Step 5: Swap the element at `start_index` with the element at the current index `i` in the loop.
  6. Step 6: Recursively call `backtrack(start_index + 1)` to explore permutations for the rest of the array.
  7. Step 7: After the recursive call returns, swap the elements back to their original positions (undo the swap) to explore other possibilities. This is the 'backtracking' step.
  8. Step 8: After the `for` loop completes, all permutations starting from `start_index` have been explored. The `backtrack` function returns.
  9. Step 9: Call `backtrack(0)` to start the permutation generation process from the beginning of the array.
  10. Step 10: Return the `result` list containing all the generated permutations.

Key Insights

  • Insight 1: Backtracking is a suitable approach for generating permutations because it explores different possibilities by making choices and undoing them if they don't lead to a valid solution.
  • Insight 2: Swapping elements in the array is an efficient way to explore different orderings during backtracking. It avoids the need for creating copies of the array repeatedly.
  • Insight 3: The base case for the recursive backtracking is when a complete permutation is formed (i.e., when the recursion reaches the end of the array). At this point, the permutation is added to the result.

Complexity Analysis

Time Complexity: O(n!)

Space Complexity: O(n*n!)

Topics

This problem involves: Array, Backtracking.

Companies

Asked at: Adobe, Amazon, American Express, Apple, Barclays, Bloomberg, Booking.com, Cisco, Citadel, Goldman Sachs, J.P. Morgan, LinkedIn, Meta, Microsoft, Microstrategy, Oracle, Qualcomm, ServiceNow, TikTok, Uber, Workday, Yahoo, Zoho, eBay.