Advertisement

Permutations II - LeetCode 47 Solution

Permutations II - Complete Solution Guide

Permutations II is LeetCode problem 47, 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 a collection of numbers, nums , that might contain duplicates, return all possible unique permutations in any order . Example 1: Input: nums = [1,1,2] Output: [[1,1,2], [1,2,1], [2,1,1]] Example 2: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Constraints: 1 <= nums.length <= 8 -10 <= nums[i] <= 10

Detailed Explanation

The problem asks us to find all unique permutations of a given list of numbers which might contain duplicates. A permutation is an arrangement of the elements in a specific order. The 'unique' part means we should not include the same permutation more than once in our result. For example, if the input is [1, 1, 2], the output should be [[1, 1, 2], [1, 2, 1], [2, 1, 1]]. The order of the permutations in the output doesn't matter. The constraints tell us the input array's length will be between 1 and 8, and each number in the array will be between -10 and 10.

Solution Approach

The provided solution uses a backtracking algorithm to generate all possible permutations. To handle duplicates effectively, the input array is first sorted. During the backtracking process, a 'used' array is maintained to track which numbers have been included in the current permutation. Crucially, before considering a number at index `i`, we check if it's the same as the previous number at `i-1`, and if the previous number hasn't been used yet. If both are true, we skip the current number to prevent generating duplicate permutations. This is the core optimization for handling duplicates.

Step-by-Step Algorithm

  1. Step 1: Sort the input array `nums`. This is necessary for the duplicate handling logic to work correctly.
  2. Step 2: Initialize an empty list `results` to store the unique permutations.
  3. Step 3: Initialize a boolean array `used` of the same length as `nums`, initialized to all `false`. This array will track which numbers are currently in the path.
  4. Step 4: Define a recursive backtracking function `backtrack(path)`:
  5. Step 5: If the length of the current `path` is equal to the length of `nums`, it means we have a complete permutation. Add a copy of `path` to `results` and return.
  6. Step 6: Iterate through the numbers in `nums` using a loop from `i = 0` to `n-1`:
  7. Step 7: If `used[i]` is `true`, it means `nums[i]` is already in the current `path`, so continue to the next number.
  8. Step 8: If `i > 0` and `nums[i] == nums[i-1]` and `!used[i-1]`, it means the current number is a duplicate of the previous one, and the previous one hasn't been used in the current path. Skip this number to avoid duplicate permutations.
  9. Step 9: Mark `used[i]` as `true` to indicate that we are now using `nums[i]` in the current `path`.
  10. Step 10: Append `nums[i]` to the current `path`.
  11. Step 11: Recursively call `backtrack(path)`.
  12. Step 12: Remove the last element from `path` (backtrack) and set `used[i]` back to `false` to explore other possibilities.
  13. Step 13: Call the `backtrack` function with an empty initial path.
  14. Step 14: Return the `results` list containing all unique permutations.

Key Insights

  • Insight 1: The core of the solution is using backtracking to explore all possible permutations.
  • Insight 2: To avoid duplicate permutations when the input contains duplicate numbers, we need to sort the input array first and then skip over duplicate numbers during the backtracking process if the previous occurrence of that number has not been used in the current permutation.
  • Insight 3: The `used` array is crucial for tracking which numbers have already been included in the current permutation being built.

Complexity Analysis

Time Complexity: O(n!)

Space Complexity: O(n!)

Topics

This problem involves: Array, Backtracking, Sorting.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, Citadel, LinkedIn, Meta, Microsoft, TikTok.