Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's time complexity of this algorithm for finding all combinations?

Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example, If n = 4 and k = 2, a solution is:

[    [2, 4],    [3, 4],    [2, 3],    [1, 2],    [1, 3],    [1, 4], ] 


Personally I think,
time complexity = O(n^k), n and k are input.
Thank you for all help.
Finally, the time complexity = O(C(n,k) * k) = O((n!/(k! * (n - k)!)) * k), n and k is input,
Since, each time when we get a combination, we need copy subList list to one_rest, which is O(k), there is C(n, k) * k.
C++
#include <vector> using namespace std;  class Solution { public:     vector<vector<int> > combine(int n, int k) {         vector<vector<int>> list;          // Input validation.         if (n < k) return list;          int start = 1;         vector<int> subList;         helper(n, k, start, list, subList);          return list;     }      void helper(int n, int k, int start,                  vector<vector<int>> &list, vector<int> &subList) {         // Base case.         if (subList.size() == k) {             vector<int> one_rest(subList);             list.push_back(one_rest);             return;         }         if (start > n) return;          for (int i = start; i <= n; i ++) {             // Have a try.             subList.push_back(i);              // Do recursion.             helper(n, k, i + 1, list, subList);              // Roll back.             subList.pop_back();         }     } }; 
like image 486
Zhaonan Avatar asked Jul 08 '14 23:07

Zhaonan


People also ask

What is the time complexity of combinations?

The time complexity is equal to the number of combinations there are.

How do you find all possible combinations?

To calculate combinations, we will use the formula nCr = n! / r! * (n - r)!, where n represents the total number of items, and r represents the number of items being chosen at a time. To calculate a combination, you will need to calculate a factorial.

What is the time complexity of finding the combinations nCr If the input array is of size n?

The time complexity analysis for Combination If an array of size n is given, then it will take O(n2) time to perform the task.

What is the time complexity of combination sum?

The time complexity is O(k * 2^N) , where k is the average length of each possible solution. Copying such a possible solution list takes O(k) time. Solution Space: Since each element is used only once (use it or not), intuitively we would say the size of the solution space is at most 2^N .


2 Answers

The complexity is O(C(n,k)) which is O(n choose k).

This ends up being equivalent to O(min(n^k, n^(n-k))).

like image 123
Nuclearman Avatar answered Sep 22 '22 01:09

Nuclearman


Since you are using lists, push_back and pop_back are O(1) operations. Also, you end up generating a valid combination exactly once. Thus, the complexity is O(n choose k).

like image 39
Pradhan Avatar answered Sep 22 '22 01:09

Pradhan