All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0046_permutations.complexity.md Maven / Gradle / Ivy

The newest version!
**Time Complexity (Big O Time):**

1. The program uses a recursive function, `permuteRecur`, to generate permutations. In the worst case, it explores all possible permutations.

2. The recursive function is called once for each element in the `nums` array, and in each call, it has a loop that iterates through the `nums` array. Therefore, the program generates `n!` permutations, where `n` is the number of elements in the input array.

3. The time complexity of generating each permutation is O(n), where `n` is the number of elements in the input array, because in each recursive call, we iterate through the `nums` array once.

4. Therefore, the overall time complexity of the program is O(n * n!), where `n` is the number of elements in the input array `nums`. This is because we generate `n!` permutations, and generating each permutation takes O(n) time.

**Space Complexity (Big O Space):**

1. The space complexity of the program is determined by the space used for the call stack during the recursion and the space used for data structures to store permutations.

2. During the recursive calls, the call stack can have a depth of up to `n`, where `n` is the number of elements in the input array `nums`. This is because the recursive function makes `n` recursive calls, each corresponding to an element in `nums`.

3. The program uses a few data structures:
   - `finalResult`: A list of lists to store permutations. In the worst case, it can contain `n!` permutations.
   - `currResult`: A list to store the current permutation being generated. Its size is at most `n`.
   - `used`: A boolean array to keep track of whether an element has been used in the current permutation. It has a size of `n`.

4. Therefore, the overall space complexity of the program is O(n + n!) in the worst case, where `n` is the number of elements in the input array `nums`. The dominant factor is `n!` due to the storage of permutations.

In summary, the time complexity of the provided program is O(n * n!), and the space complexity is O(n + n!) in the worst case, where `n` is the number of elements in the input array `nums`.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy