<p>Given two integers <code>n</code> and <code>k</code>, return <em>all possible combinations of</em> <code>k</code> <em>numbers chosen from the range</em> <code>[1, n]</code>.</p>

<p>You may return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 4, k = 2
<strong>Output:</strong> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
<strong>Explanation:</strong> There are 4 choose 2 = 6 total combinations.
Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1, k = 1
<strong>Output:</strong> [[1]]
<strong>Explanation:</strong> There is 1 choose 1 = 1 total combination.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 20</code></li>
	<li><code>1 &lt;= k &lt;= n</code></li>
</ul>

class Solution {
public:
void vectorPopFront(std::vector<int>& vec) {
	for (int i = 1; i < vec.size(); i++) vec[i - 1] = vec[i];
	vec.resize(vec.size()-1);
}

void combineBacktrackung(std::vector<std::vector<int>>& res, std::vector<int> subRes, std::vector<int> range, int k) {
	if (range.size() == 0 || subRes.size() + range.size() < k) return;
	
	std::vector<int> newSubRes = subRes;
	newSubRes.resize(subRes.size() + 1);

	while (!range.empty()) {
		newSubRes[newSubRes.size() - 1] = range.front();
		vectorPopFront(range);

		if (newSubRes.size() == k) {
			res.push_back(newSubRes);

			continue;
		}

		combineBacktrackung(res, newSubRes, range, k);
	}
}

std::vector<std::vector<int>> combine(int n, int k) {
	std::vector<int> range = {};
	range.resize(n);
	for (int i = 0; i < range.size(); i++) range[i] = (i + 1);

	std::vector<std::vector<int>> res = {};

	if (n == k) {
		res.push_back(range);
		return res;
	}

	combineBacktrackung(res, std::vector<int>(), range, k);

	return res;
}
};
124887