badam_milk's blog

By badam_milk, history, 4 years ago, In English

Hello everyone,

I have successfully managed to write a recursive program to print all possible combinations of getting a coin change, let's say value:

void print_possible_change(vector<int> &change, int beginWith, int value, vector<vector<int>> &res, vector<int> &temp){
	if(value == 0){
		res.push_back(temp);
	}

	for(int i = beginWith; i < change.size() && change[i] <= value; ++i){
		temp.push_back(change[i]);
		print_possible_change(change, i, value - change[i], res, temp);
		temp.pop_back();
	}
}

The final result of all combinations is stored in res.

This approach clearly runs in exponential time.

Is there an approach that runs in quadratic time or less?

Thank You.

  • Vote: I like it
  • +7
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +21 Vote: I do not like it

Any solution that you will find will have complexity at least equal to the number of possible solutions(Since you need to print them, you must enumerate each of those solutions). As far as I know, the number of solutions is exponential, hence is the time complexity.