<p>Given an array of <strong>distinct</strong> integers <code>candidates</code> and a target integer <code>target</code>, return <em>a list of all <strong>unique combinations</strong> of </em><code>candidates</code><em> where the chosen numbers sum to </em><code>target</code><em>.</em> You may return the combinations in <strong>any order</strong>.</p>

<p>The <strong>same</strong> number may be chosen from <code>candidates</code> an <strong>unlimited number of times</strong>. Two combinations are unique if the <span data-keyword="frequency-array">frequency</span> of at least one of the chosen numbers is different.</p>

<p>The test cases are generated such that the number of unique combinations that sum up to <code>target</code> is less than <code>150</code> combinations for the given input.</p>

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

<pre>
<strong>Input:</strong> candidates = [2,3,6,7], target = 7
<strong>Output:</strong> [[2,2,3],[7]]
<strong>Explanation:</strong>
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.
</pre>

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

<pre>
<strong>Input:</strong> candidates = [2,3,5], target = 8
<strong>Output:</strong> [[2,2,2,2],[2,3,3],[3,5]]
</pre>

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

<pre>
<strong>Input:</strong> candidates = [2], target = 1
<strong>Output:</strong> []
</pre>

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

<ul>
	<li><code>1 &lt;= candidates.length &lt;= 30</code></li>
	<li><code>2 &lt;= candidates[i] &lt;= 40</code></li>
	<li>All elements of <code>candidates</code> are <strong>distinct</strong>.</li>
	<li><code>1 &lt;= target &lt;= 40</code></li>
</ul>

class Solution {
    unordered_map<string, vector<vector<int>>> dp;

    vector<vector<int>> find(int ind, int target, vector<int>& cand) {
        // Base case: if the target is achieved, return a list containing an empty combination
        if (target == 0) return {{}};
        
        // If index reaches the end of the list, return an empty list
        if (ind == cand.size()) return {};

        // Create a unique key for the current state (ind, target)
        string key = to_string(ind) + "-" + to_string(target);

        // If the result for this state has already been computed, return it
        if (dp.find(key) != dp.end()) return dp[key];

        // Option 1: Exclude the current element and move to the next
        vector<vector<int>> exclude = find(ind + 1, target, cand);

        // Option 2: Include the current element (if it doesn't exceed the target)
        vector<vector<int>> include;
        if (target >= cand[ind]) {
            vector<vector<int>> includeRest = find(ind, target - cand[ind], cand);
            for (auto& combination : includeRest) {
                combination.push_back(cand[ind]);
                include.push_back(combination);
            }
        }

        // Merge the results of including and excluding the current element
        exclude.insert(exclude.end(), include.begin(), include.end());

        // Store the result in the memoization table and return it
        dp[key] = exclude;
        return dp[key];
    }

public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        dp.clear();  // Clear the memoization table
        return find(0, target, candidates);
    }
};

32500