<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:
    // Try to add numbers starting s down to 0 to the existent vector
    // with k and n parameters
    // Return the set of the successful child vectors
    vector<vector<int>> add_ok(const vector<int>& prev, int s) {
        vector<vector<int>> rval;
        // count the current sum (<n)
        const int sum = accumulate(prev.begin(), prev.end(),0);
        // try all the numbers [s;0)
        while(s > 0) {
          // if new sum is n and k reached, add new child vector to the set
          if((sum + s) == m_n && prev.size() ==(m_k-1)){
            vector<int> r{prev};
            r.push_back(s);
            rval.push_back(r);
          } else
          // if n is still not reached, create new vector with s
          // and use recursion to continue the search with s-1
          if((sum+s) < m_n && prev.size() < m_k) {
              vector<int> r{prev};
              r.push_back(s);
              vector<vector<int>> tval = add_ok(r,s-1);
              rval.insert(rval.end(), tval.begin(), tval.end());
          }
          // to the next digit
          --s;
        }
        return rval;
    }

    vector<vector<int>> combinationSum3(int k, int n) {
        // start positions for all possible k
        vector<int> kk{0,1,3,6,10,15,21,28,36};
        // our greatest digit to check
        int start = n - kk[k-1];
        // correction
        if(start > 9) {
            start = 9;
        }
        m_n = n, m_k = k;
        return add_ok({}, start);
    }
    
    private:
    int m_n, m_k; // to save the stack
};

9300