Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

How to print all possible combinations of getting a required coin change in quadratic time or less?

Правка en1, от badam_milk, 2020-04-18 10:04:09

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();
	}
}

This approach is clearly run in exponential time.

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

Thank You.

Теги #dp, #recursion, #combinatorics, #combination

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en3 Английский badam_milk 2020-04-18 10:05:33 4 Tiny change: ' approach is clearly run in expone' -> ' approach clearly runs in expone'
en2 Английский badam_milk 2020-04-18 10:04:51 64
en1 Английский badam_milk 2020-04-18 10:04:09 764 Initial revision (published)