2

The problem at hand is : Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

I have this code with an ok run time but it seems that the push function is not working, I am not sure why bc it all makes sense in general

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]]

Constraints: 
1 <= nums.length <= 6
-10 <= nums[i] <= 10
All the integers of nums are unique.


var permute = function(nums) {
    if (nums == null) {
        return []
    }
    return getPermutations(nums);
};

function getPermutations(arr) {
    var set = {}
    var size = factorial(arr.length)
        var result = []
    while (Object.keys(set).length < size) {
        for (var i = 0; i < arr.length && result.length < size; i++) {
            for (var j = arr.length - 1; j >= 0 && result.length < size; j--) {
                if (!set[arr]) {
                    set[arr] = 1
                    console.log(arr) //clearly see the permutations printed 
                    result.push(arr) //why is this not working...
                }
                arr = swap(arr,i,j)
            }
        }
    }
    return result
}

function swap(arr,i,j) {
    var tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
    return arr
}

function factorial(n) {
    if (n == 0 || n == 1) { 
        return 1
    }
    return n*factorial(n-1)
}
masilv17
  • 129
  • 9

1 Answers1

2

You're pushing the same array multiple times into the result array.

You can fix this by creating a copy of the arr array before pushing it.
(So the code after it can't mutate the array again)

So instead of result.push(arr) you could use one of those examples:

// using splash operator
result.push([...arr]);

// Array#slice()
result.push(arr.slice());

// Array.from()
result.push(Array.from(arr));

// etc...

Working Example:

var permute = function(nums) {
    if (nums == null) {
        return []
    }
    return getPermutations(nums);
};

function getPermutations(arr) {
    var set = {}
    var size = factorial(arr.length)
        var result = []
    while (Object.keys(set).length < size) {
        for (var i = 0; i < arr.length && result.length < size; i++) {
            for (var j = arr.length - 1; j >= 0 && result.length < size; j--) {
                if (!set[arr]) {
                    set[arr] = 1
                    console.log(arr) //clearly see the permutations printed
                    result.push([...arr]) //why is this not working...
                }
                arr = swap(arr,i,j)
            }
        }
    }
    return result
}

function swap(arr,i,j) {
    var tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
    return arr
}

function factorial(n) {
    if (n == 0 || n == 1) { 
        return 1
    }
    return n*factorial(n-1)
}

console.log(permute([1,2,3]));

This question could also be a good read, it contains a lot of examples for how to efficiently calculate permutations in javascript.

Turtlefight
  • 9,420
  • 2
  • 23
  • 40