# Permutations

{% embed url="<https://leetcode.com/problems/permutations>" %}

### Problem

> Given an array `nums` of distinct integers, return *all the possible permutations*. You can return the answer in **any order**.
>
> &#x20;
>
> **Example 1:**
>
> <pre><code>Input: nums = [1,2,3]
> <strong>Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
> </strong></code></pre>
>
> **Example 2:**
>
> <pre><code>Input: nums = [0,1]
> <strong>Output: [[0,1],[1,0]]
> </strong></code></pre>
>
> **Example 3:**
>
> <pre><code>Input: nums = [1]
> <strong>Output: [[1]]
> </strong></code></pre>

### Pseudocode

```
- dfs with a for loop within
    - permutation with backtracking
    
- same as combination sum, don't understand this intuitively
```

### Solution

```javascript
// from solutions
// https://leetcode.com/problems/permutations/solutions/685868/dfs-backtracking-python-java-javascript-picture/
// good illustration on how to approach the problem
// time O(N^N), has to find every permutation
var permute = function (nums) {
  let res = [];
  dfs(nums, [], Array(nums.length).fill(false), res);
  return res;
};

function dfs(nums, path, used, res) {
  // console.log(path)
  if (path.length == nums.length) {
    // make a deep copy since otherwise we'd be append the same list over and over
    res.push(Array.from(path));
    return;
  }
  for (let i = 0; i < nums.length; i++) {
    // skip used nums
    if (used[i]) continue;
    // add letter to permutation, mark letter as used
    path.push(nums[i]);
    used[i] = true;
    dfs(nums, path, used, res);
    // remove letter from permutation, mark letter as unused
    path.pop();
    used[i] = false;
  }
}

```

### Time and Space Complexity

#### Time

* What did the code do
* Total -

#### Space

* What did the code do
* Total -


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://grind75-notes.gitbook.io/notes/week-5/permutations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
