time and space complexity of finding combination (

2019-06-13 16:09发布

问题:

Whats the worst case time and space complexity of different algorithms to find combination i.e. nCr Which algorithm is the best known solution in terms of time/space complexity?

回答1:

O(n!) is the time complexity to generate all combinations one by one.

To find how many combinations are there, we can use this formula:

nCr = n! / ( r! * (n-r)! )

As @beaker mentioned, this count can be calculated in O(1) time (i.e., constant time).