<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p>

<ul>
	<li>Only numbers <code>1</code> through <code>9</code> are used.</li>
	<li>Each number is used <strong>at most once</strong>.</li>
</ul>

<p>Return <em>a list of all possible valid combinations</em>. The list must not contain the same combination twice, and the combinations may be returned in any order.</p>

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

<pre>
<strong>Input:</strong> k = 3, n = 7
<strong>Output:</strong> [[1,2,4]]
<strong>Explanation:</strong>
1 + 2 + 4 = 7
There are no other valid combinations.</pre>

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

<pre>
<strong>Input:</strong> k = 3, n = 9
<strong>Output:</strong> [[1,2,6],[1,3,5],[2,3,4]]
<strong>Explanation:</strong>
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
</pre>

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

<pre>
<strong>Input:</strong> k = 4, n = 1
<strong>Output:</strong> []
<strong>Explanation:</strong> There are no valid combinations.
Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 &gt; 1, there are no valid combination.
</pre>

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

<ul>
	<li><code>2 &lt;= k &lt;= 9</code></li>
	<li><code>1 &lt;= n &lt;= 60</code></li>
</ul>

class Solution {
public:
    std::vector<std::vector<int>> combinationSum3(int k, int n) {
        std::vector<std::vector<int>> result;
        std::vector<int> combination;
        std::vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        backtrack(nums, k, n, 0, combination, result);
        return result;
    }

private:
    void backtrack(const std::vector<int>& nums, int k, int n, int start, 
                   std::vector<int>& combination, std::vector<std::vector<int>>& result) {
        if (combination.size() == k && n == 0) {
            result.push_back(combination);
            return;
        }
        for (int i = start; i < nums.size(); ++i) {
            if (n < nums[i]) continue; // Skip if current number is greater than remaining sum
            combination.push_back(nums[i]);
            backtrack(nums, k, n - nums[i], i + 1, combination, result);
            combination.pop_back(); // Remove last element to try the next candidate
        }
    }
};
// class Solution {
// public:
//     int findCombo(int kp, map <string, string> &m ,int k, int n, vector<vector<int>> &res, int sum, int start, int *idx, string s, int *len) {
//         int ret = -1;
//         if (s != "" && m[s].length() != 0) {
//             start = (m[s][m[s].length() -1] - '0') + 1;
            
//         }
//         if (start == 10) {
//             return -1;
//         }

//         for (int i = start; i < 10; i++) {
//             sum += i;
//             if (k == 1) {
//                 if (sum == n) {
//                     res.push_back({i});
//                     return i;
//                 } else {
//                 }
//             } else {
//                 s += ('0' + i);
//                 int result = findCombo(kp, m, k-1, n, res, sum, i + 1, idx, s, len);
//                 if (result != -1) {
//                     res[*idx].push_back(i);
//                     if (s.length() >= *len) {
//                         m[s] += ('0' + result);
//                     }
                    
//                     return i;
//                 }
//                 s.pop_back();
//             }
                
                
//             sum -= i;
//         }

//         return ret;
//     }
   
//     vector<vector<int>> combinationSum3(int k, int n) {
//         vector<vector<int>> res;
//         int sum = 0;
//         int idx = 0;
//         map <string, string> m;
//         int len = k - 1;

//         while ( findCombo(k, m, k, n, res, sum,  1, &idx, "", &len) != -1 ) {
//             idx++;
//         }

//         return res;
        
//     }
// };
7700